源码:
<script type="text/javascript">
//<![CDATA[
(function(){
var appid = ‘cyqU2kr6K‘;
var conf = ‘prod_5a7b238ba0f6502e5d6be14424b20ded‘;
var doc = document,
s = doc.createElement(‘script‘);
s.id = ‘changyan_mobile_js‘;
h = doc.getElementsByTagName(‘head‘)[0] || doc.head || doc.documentElement;
s.type = ‘text/javascript‘;
s.charset = ‘utf-8‘;
s.src = ‘http://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=‘+appid+‘&conf=‘+conf;
h.insertBefore(s,h.firstChild);
})();
//]]>
</script>
生成:
<script id="changyan_mobile_js" type="text/javascript" charset="utf-8" src="http://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=cyqU2kr6K&conf=prod_5a7b238ba0f6502e5d6be14424b20ded"></script>
function trans(html){
var o = document.createElement(‘div‘),
t = document.createTextNode(html);
o.appendChild(t);
return o.innerHTML;
}
var txt = trans(‘<div><a href="">a<b>b</b>c</a></div>‘);
alert(txt)
这样可以直接给html转义