innerHTML中的js运行

innerHTML后js一直无法运行,困扰了我好久

最近又要用到这个,还要在innerHTML后去执行里面的js,网上搜到各种方法,都不行

后来想到ExtJs中,有个loadScripts参数,去看了下源码,发现每个el中有个update方法,正是我想要的,嘿嘿 ,问题解决

		update : function(html, loadScripts, callback) {
			if (typeof html == "undefined") {
				html = "";
			}
			if (loadScripts !== true) {
				this.dom.innerHTML = html;
				if (typeof callback == "function") {
					callback();
				}
				return this;
			}
			var id = Ext.id();
			var dom = this.dom;

			html += '<span id="' + id + '"></span>';

			E.onAvailable(id, function() {
						var hd = document.getElementsByTagName("head")[0];
						var re = /(?:<script([^>]*)?>)((\n|\r|.)*?)(?:<\/script>)/ig;
						var srcRe = /\ssrc=([\'\"])(.*?)\1/i;
						var typeRe = /\stype=([\'\"])(.*?)\1/i;

						var match;
						while (match = re.exec(html)) {
							var attrs = match[1];
							var srcMatch = attrs ? attrs.match(srcRe) : false;
							if (srcMatch && srcMatch[2]) {
								var s = document.createElement("script");
								s.src = srcMatch[2];
								var typeMatch = attrs.match(typeRe);
								if (typeMatch && typeMatch[2]) {
									s.type = typeMatch[2];
								}
								hd.appendChild(s);
							} else if (match[2] && match[2].length > 0) {
								if (window.execScript) {
									window.execScript(match[2]);
								} else {
									window.eval(match[2]);
								}
							}
						}
						var el = document.getElementById(id);
						if (el) {
							Ext.removeNode(el);
						}
						if (typeof callback == "function") {
							callback();
						}
					});
			dom.innerHTML = html.replace(
					/(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig, "");
			return this;
		},


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值