具有特殊字符的InnerHTML会修剪数据 [英] InnerHTML with special character is trimming the data

查看:108
本文介绍了具有特殊字符的InnerHTML会修剪数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有特殊字符的InnerHTML正在修剪数据。

InnerHTML with special character is trimming the data.

elem.innerHTML = displayedObjects.name;

其中 displayedObjects.name 包含一个字符串例如 Test& string 。上面的语句仅分配 Test

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

在这里可以做什么?

What could be done here?

推荐答案

这是因为 Test& string 实际上不是有效的HTML,因为& HTML实体的转义字符。如果编码正确,则将改为 Test& string

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.

如果您只是尝试设置元素的文本,建议您使用 innerText 代替:

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

elem.innerText = displayedObjects.name;

这篇关于具有特殊字符的InnerHTML会修剪数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆