小编典典

HTML实体解码

javascript

如何使用JavaScript或JQuery编码和解码HTML实体?

var varTitle = "Chris' corner";

我希望它是:

var varTitle = "Chris' corner";

阅读 312

收藏
2020-04-25

共1个答案

小编典典

您可以尝试类似:

var Title = $('<textarea />').html("Chris&apos; corner").text();

console.log(Title);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

更具交互性的版本:

$('form').submit(function() {

  var theString = $('#string').val();

  var varTitle = $('<textarea />').html(theString).text();

  $('#output').text(varTitle);

  return false;

});


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form action="#" method="post">

  <fieldset>

    <label for="string">Enter a html-encoded string to decode</label>

    <input type="text" name="string" id="string" />

  </fieldset>

  <fieldset>

    <input type="submit" value="decode" />

  </fieldset>

</form>



<div id="output"></div>
2020-04-25