如何使用JavaScript或JQuery编码和解码HTML实体?
var varTitle = "Chris' corner";
我希望它是:
var varTitle = "Chris' corner";
您可以尝试类似:
var Title = $('<textarea />').html("Chris' 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>