字符串反转义的问题,困扰了我一天了! 本帖最后由 webstandards 于 2014-01-10 00:27:44 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 什么编程语言?php 用htmlspecialchars_decode($str);JS代码参考:http://phpjs.org/functions/htmlspecialchars_decode/function htmlspecialchars_decode (string, quote_style) { var optTemp = 0, i = 0, noquotes = false; if (typeof quote_style === 'undefined') { quote_style = 2; } string = string.toString().replace(/</g, '<').replace(/>/g, '>'); var OPTS = { 'ENT_NOQUOTES': 0, 'ENT_HTML_QUOTE_SINGLE': 1, 'ENT_HTML_QUOTE_DOUBLE': 2, 'ENT_COMPAT': 2, 'ENT_QUOTES': 3, 'ENT_IGNORE': 4 }; if (quote_style === 0) { noquotes = true; } if (typeof quote_style !== 'number') { quote_style = [].concat(quote_style); for (i = 0; i < quote_style.length; i++) { if (OPTS[quote_style[i]] === 0) { noquotes = true; } else if (OPTS[quote_style[i]]) { optTemp = optTemp | OPTS[quote_style[i]]; } } quote_style = optTemp; } if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) { string = string.replace(/�*39;/g, "'"); } if (!noquotes) { string = string.replace(/"/g, '"'); } string = string.replace(/&/g, '&'); return string;} <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script type="text/javascript">window.onload=function(){var itemContent="<p>8864的萨芬的萨芬55</p>"+"<p>8864的萨芬的萨芬55</p>"var k=document.createTextNode(itemContent);itemContenter.appendChild(k);}</script></head><body><div id="itemContenter"></div></body></html>建个textNode试试 <div class="itemContent" id="itemContenter"><p>8864的萨芬的萨芬55</p></div><script> function decodehtml(s) { return s.replace(/>/gi, '>').replace(/</gi, '<'); } var d = document.getElementById('itemContenter'); d.innerHTML = decodehtml(d.innerHTML);</script> 从数据库里读出来的字符串是已经被转义过了的,要在浏览器显示时再把它反转义。没进行反转义是下面这个效果。<div class="itemContent" id="itemContenter"><p>8864的萨芬的萨芬55</p></div>我用了上面的反转义函数过了一遍,失败。<script type="text/javascript">var itemContent="<p>8864的萨芬的萨芬55</p><p>8864的萨芬的萨芬55</p>"itemContent=itemContent.replace(/>/g,">").replace(/</g,"<");itemContenter.innerHTML=itemContent;</script>---------->函数写成这样就没问题了。<script type="text/javascript">itemContenter.innerText = strFromDB <----从数据库得到的原始数据itemContenter.innerHTML=itemContenter.innerText;</script> 如果可以,最好還是從源頭找起,把數據從存入資料庫的時候或從資料庫取出數據的時候,就把換行改成<br> 关于弹出窗口 把分都散完。。寻求js模块化的答案 js 如何向列表菜单添加一个或多个选项 javascript速度 用JS如何判断页面是否加了W3C声明 关于打开子窗口的问题 js alert 对象 与遍历 的 问题 求解答 返回上一页的刷新问题.请大家帮忙指点一下!!!谢谢!!! how to input check box in text area or select control area? jqplot 中的shapeRenderer 画任意矩形,有知道的吗? CKEditor 上传图片-》高级 样式类名称 怎么给默认样式 请教个高级js的问题
JS代码参考:http://phpjs.org/functions/htmlspecialchars_decode/function htmlspecialchars_decode (string, quote_style) {
var optTemp = 0,
i = 0,
noquotes = false;
if (typeof quote_style === 'undefined') {
quote_style = 2;
}
string = string.toString().replace(/</g, '<').replace(/>/g, '>');
var OPTS = {
'ENT_NOQUOTES': 0,
'ENT_HTML_QUOTE_SINGLE': 1,
'ENT_HTML_QUOTE_DOUBLE': 2,
'ENT_COMPAT': 2,
'ENT_QUOTES': 3,
'ENT_IGNORE': 4
};
if (quote_style === 0) {
noquotes = true;
}
if (typeof quote_style !== 'number') {
quote_style = [].concat(quote_style);
for (i = 0; i < quote_style.length; i++) {
if (OPTS[quote_style[i]] === 0) {
noquotes = true;
} else if (OPTS[quote_style[i]]) {
optTemp = optTemp | OPTS[quote_style[i]];
}
}
quote_style = optTemp;
}
if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
string = string.replace(/�*39;/g, "'");
}
if (!noquotes) {
string = string.replace(/"/g, '"');
}
string = string.replace(/&/g, '&');
return string;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function(){
var itemContent="<p>8864的萨芬的萨芬55</p>"+
"<p>8864的萨芬的萨芬55</p>"
var k=document.createTextNode(itemContent);
itemContenter.appendChild(k);
}
</script>
</head><body>
<div id="itemContenter"></div>
</body>
</html>
建个textNode试试
<p>
8864的萨芬的萨芬55
</p>
</div>
<script>
function decodehtml(s) {
return s.replace(/>/gi, '>').replace(/</gi, '<');
}
var d = document.getElementById('itemContenter');
d.innerHTML = decodehtml(d.innerHTML);
</script>
<div class="itemContent" id="itemContenter">
<p>
8864的萨芬的萨芬55
</p>
</div>我用了上面的反转义函数过了一遍,失败。
<script type="text/javascript">
var itemContent="<p>8864的萨芬的萨芬55</p>
<p>8864的萨芬的萨芬55</p>"
itemContent=itemContent.replace(/>/g,">").replace(/</g,"<");
itemContenter.innerHTML=itemContent;
</script>---------->函数写成这样就没问题了。
<script type="text/javascript">
itemContenter.innerText = strFromDB <----从数据库得到的原始数据
itemContenter.innerHTML=itemContenter.innerText;
</script>