[原]解决innerHTML时JS不生效
写了一个简单的函数..作用是解决innerHTML时JS不生效..基于网上某个类修改...
兼容ie6+..Firefox..其它未测!
1 function set_innerHTML(el, htmlCode) {
2 var ua = navigator.userAgent.toLowerCase();
3 if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
4 htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
5 htmlCode = htmlCode.replace(/<script([^>]*)>/gi,'<script$1 defer>');
6 el.innerHTML = htmlCode;
7 el.removeChild(el.firstChild);
8 } else {
9 var html = htmlCode.replace( /<script[^>]*>([\s\S]*?)<\/script>/mgi, "" );
10 var code = htmlCode.replace( /[\s\S]*?<script[^>]*>([\s\S]*?)<\/script>[\s\S]*?/mgi, "$1" );
11 //.和[\s\S] 不一样
12 el.innerHTML = html;
13 var wc = T$('wc1217');
14 if(wc)
15 wc.parentNode.removeChild(wc);
16 script = document.createElement("script");
17 script.defer = true;
18 script.type = "text/javascript";
19 script.id = "wc1217";
20 script.text = code;
21 setTimeout(function(){document.getElementsByTagName('body').item(0).appendChild( script )},1000);
22 }
23 }
24 //T$就是document.getElementByid
作者:wc1217 发表于2011-12-12 22:01:24 原文链接
阅读:21 评论:0 查看评论