InnerHTML with special character is trimming the data

╄→гoц情女王★ 提交于 2019-12-11 05:41:38

问题


InnerHTML with special character is trimming the data.

elem.innerHTML = displayedObjects.name;

here the displayedObjects.name contains a string like Test&string. The above statement is assigning the value only Test,

What could be done here?


回答1:


That's because Test&string isn't actually valid HTML, because & is an escape character for an HTML entity. If it were properly encoded, it would be Test&string instead.

If you're just trying to set the text of an element, I'd suggest you use innerText instead:

elem.innerText = displayedObjects.name;


来源:https://stackoverflow.com/questions/20604572/innerhtml-with-special-character-is-trimming-the-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!