[size=large]当你写了一个内弹窗口,
用ajax获取远程的html.
然后直接用innerHTML,插到网页中.
这时候你想针对加载内容运行一段js,
但是innerHTML中插入的似乎不会被执行,
怎么办?
当然,你可以用一个统一的js文件包含所有的函数,预先加载,
但是,这样维护起来很麻烦,由其不止你一个人写代码的时候
再去ajax加载一段js,Oh,好麻烦.
还是用我这个函数吧
传入htm
返回值.htm 就返回不含js的htm代码
返回值.js 就执行htm中的js
需要注意的是,要先将htm用innerHTML插入到网页中再去运行js脚本,否则getElementById是不生效的
最后不要忘记setTimout 0 的小技巧
setTimeout('document.getElementById("signin_password").focus()',0)
____________________________________________
function js_parser(htm){
var tag="script>",begin="<"+tag,end="</"+tag,pos=pos_pre=0,result=script="";
while(
(pos=htm.indexOf(begin,pos))+1
){
result+=htm.substring(pos_pre,pos);
pos+=8;
pos_pre=htm.indexOf(end,pos);
if(pos_pre<0){
alert("标签未闭合");
break;
}
script+=htm.substring(pos,pos_pre)+";";
pos_pre+=9;
}
result+=htm.substring(pos_pre,htm.length);
return {
htm:result,
js:function(){eval(script)}
};
}
[/size]
用ajax获取远程的html.
然后直接用innerHTML,插到网页中.
这时候你想针对加载内容运行一段js,
但是innerHTML中插入的似乎不会被执行,
怎么办?
当然,你可以用一个统一的js文件包含所有的函数,预先加载,
但是,这样维护起来很麻烦,由其不止你一个人写代码的时候
再去ajax加载一段js,Oh,好麻烦.
还是用我这个函数吧
传入htm
返回值.htm 就返回不含js的htm代码
返回值.js 就执行htm中的js
需要注意的是,要先将htm用innerHTML插入到网页中再去运行js脚本,否则getElementById是不生效的
最后不要忘记setTimout 0 的小技巧
setTimeout('document.getElementById("signin_password").focus()',0)
____________________________________________
function js_parser(htm){
var tag="script>",begin="<"+tag,end="</"+tag,pos=pos_pre=0,result=script="";
while(
(pos=htm.indexOf(begin,pos))+1
){
result+=htm.substring(pos_pre,pos);
pos+=8;
pos_pre=htm.indexOf(end,pos);
if(pos_pre<0){
alert("标签未闭合");
break;
}
script+=htm.substring(pos,pos_pre)+";";
pos_pre+=9;
}
result+=htm.substring(pos_pre,htm.length);
return {
htm:result,
js:function(){eval(script)}
};
}
[/size]