jquery.html页面不显示,jQuery html()方法显示不了内容的问题

$("#content").html(data.content);

$("#content")[0].innerHTML = data.content;

今天遇到jquery中的html方法使用不了,只能用完最基本的innerHTML把内容展示出来。执行html()方法时,它会执行里面的script,会用jquery的globalEval方法执行,我页面执行下面这段script后,变空白了。

// 执行到globalEval时会转成下面的data,然后执行

var data = "var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_5752922'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s17.cnzz.com/stat.php%3Fid%3D5752922%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));";

window[ "eval" ].call( window, data );

jquery的globalEval方法,执行后,页面内容变空白了。

globalEval: function( data ) {

if ( data && jQuery.trim( data ) ) {

// We use execScript on Internet Explorer

// We use an anonymous function so that context is window

// rather than jQuery in Firefox

( window.execScript || function( data ) {

window[ "eval" ].call( window, data );

} )( data );

}

}

function testHtml(){

var data = "var cnzz_protocol = ((\"https:\" == document.location.protocol) ? \" https://\" : \" http://\");document.write(unescape(\"%3Cspan id='cnzz_stat_icon_5752922'%3E%3C/span%3E%3Cscript src='\" + cnzz_protocol + \"s17.cnzz.com/stat.php%3Fid%3D5752922%26show%3Dpic' type='text/javascript'%3E%3C/script%3E\"));";

console.log(data);

window[ "eval" ].call( window, data );

}

document.write("");

实际上就是执行这段代码,把页面内容搞没了,也可以直接执行testHtml()方法,页面内容也没了。

看了源码后,感觉innerHTML方法够用的话,就不要用jquery中的html方法了,里面会执行各种判断。不需要执行内容里的script直接用innerHTML,需要执行内容里的script时用html方法。

对于URL特殊字符用unescape即可转成正常的url。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值