最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - InnerHTML with special character is trimming the data - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Dec 16, 2013 at 15:52 p.s.w.g 149k31 gold badges305 silver badges337 bronze badges asked Dec 16, 2013 at 6:13 user3106224user3106224 112 silver badges3 bronze badges 1
  • 2 Hm, somebody already edited this, Why you return to plain text again? – vp_arth Commented Dec 16, 2013 at 6:16
Add a ment  | 

1 Answer 1

Reset to default 7

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;
发布评论

评论列表(0)

  1. 暂无评论