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;
},