转义注意的问题

function htmlEscape(str) {
if(str && typeof(str) == "string"){
return str.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/'/g, '&#039;')
.replace(/"/g, '&quot;')
}
return str;
}

常见的有:&(and)、"(双引号)、'(单引号)、<(小于号)、>(大于号),这些在 (x)HTML 文档内容中应该分别转换成:&amp;、&quot;、&#039;、&lt; 和 &gt;。

为什么上面单引号的转义和别的特殊字符不同呢?为什么唯独单引号用的是实体编号(&#039;),而其它的用的是实体名称(&apos;)呢?

 IE 浏览器暂时只支持单引号的实体编号。

这也是 PHP htmlspecialchars 函数把单引号转义成 &#039;,而不是 &apos; 的原因。

另外,百度也不支持单引号的实体名称.

猜你喜欢

转载自www.cnblogs.com/ey-151210/p/9481593.html
今日推荐