zoukankan      html  css  js  c++  java
  • [原]解决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 查看评论
  • 相关阅读:
    Linux 命令[5]:rmdir
    Linux 命令[4]:pwd,date
    Linux 命令[0]:起航
    Linux 命令[3]:cd
    vscode插件
    Object.freeze()
    插件
    前端开发调试线上代码
    前端自动化测试是浪费时间还是节约时间?
    踩坑之用lrz插件进行图片压缩
  • 原文地址:https://www.cnblogs.com/wc1217/p/2387571.html
Copyright © 2011-2022 走看看