I have a HTML input text, and its values are populated from a related div. My problem is that the div contains characters like &
which will display correcly as '&'
sign in div but when copied to text box the text '&'
will be dispalyed
How can i convert &
to &
and '<'
to '<'
, ' '
to ' '
???
You thus want to unescape HTML entities . With plain JS you can use this snippet:
function unescapeHTML(html) {
var div = document.createElement("DIV");
div.innerHTML = html;
return ("innerText" in div) ? div.innerText : div.textContent; // IE | FF
}
And with jQuery the following one:
function unescapeHTML(html) {
return $("<div />").html(html).text();
}
But you can also just fix this problem during the step that you "copy" the div's content into the input element's value. Instead of grabbing HTML, just grab text . Instead of element.innerHTML
, that'll be element.innerText
for IE or element.textContent
for real browsers.
No, you can't and shouldn't do this (reliably) with regex.
我不确定您如何访问数据,但可能的解决方案可能是使用innerText
属性而不是innerHtml
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.