如何让这段插入的innerHTML 里动态赋予的函数跑起来!
<BODY>
<input type= "button " onclick= "show(); " value= "Click "/>
<div id= "div1 ">
</div>
<div id= "div2 ">
<ul>
<li> Test1 </li>
<li> Test1 </li>
<li> Test1 </li>
</ul>
</div>
<script>
function $(obj){
return document.getElementById(obj);
}
function show(){
$( "div1 ").innerHTML = $( "div2 ").innerHTML; //填充到div1
bindEvt($( "div2 "));
}
function bindEvt(obj){
var lis = obj.getElementsByTagName( "li ");
for (var i = 0; i < lis.length; i++){
//alert(lis[i].innerHTML);
lis[i].onclick = function (){ //给innerHTML的Li绑定事件 但是不运行,怎么让它运行起来呢
alert(lis[i].innerHTML);
}
}
}
</script>
</BODY>
------解决方案-------------------- <BODY>
<input type= "button " onclick= "show(); " value= "Click "/>
<div id= "div1 ">
</div>
<div id= "div2 ">
<ul>
<li> Test1 </li>
<li> Test2 </li>
<li> Test3 </li>
</ul>
</div>
<script>
function $(obj){
return document.getElementById(obj);
}
function show(){
$( "div1 ").innerHTML = $( "div2 ").innerHTML; //填充到div1
bindEvt($( "div2 "));
}
function bindEvt(obj){
var lis = obj.getElementsByTagName( "li ");
for (var i = 0; i < lis.length; i++){
//alert(lis[i].innerHTML);
lis[i].onclick = function (){ //给innerHTML的Li绑定事件 但是不运行,怎么让它运行起来呢
alert(this.innerHTML);
}
}
}
</script>
</BODY>
------解决方案-------------------- <script language= "JavaScript1.2 ">
function $(obj){
return document.getElementById(obj);
}
function show(){
$( "div1 ").innerHTML = $( "div2 ").innerHTML; //填充到div1
bindEvt($( "div2 "));
}
function bindEvt(obj){
var lis = document.all.tags( "li ");
//alert(lis[i].innerHTML);
for (var i = 0; i < lis.length; i++){
lis[i].onclick = function (){ //给innerHTML的Li绑定事件 但是不运行,怎么让它运行起来呢
alert(event.srcElement.innerHTML);
}
}
}
</script>
<BODY>
<FORM METHOD=POST ACTION= " ">
<input type= "button " onclick= "show(); " value= "Click ">
<div id= "div1 "> </div>
<div id= "div2 ">
<ul>
<li> Test1 </li>
<li> Test2 </li>
<li> Test3 </li>
</ul>
</div>
</FORM>
"alert(lis[i].innerHTML); "
这样子写是错误的,
当给li加了一个onclick的时候,
点击就不存在i的值了.
------解决方案--------------------alert(lis[i].innerHTML)
=>
alert(this.innerHTML)
------解决方案--------------------