关于动态设置html元素的事件的小问题
function addDiv()
{ var newSelect=document.createElement( " <select "> );
var newOption=document.createElement( "option ");
newOption.innerHTML= "ddd ";
newSelect.onFocus= "ee() ";
document.getElementById( "myDiv ").innerHTML=newSelect.outerHTML;
}
function ee(){alert( "dd ")}
<input type= "button " onClick= "addDiv() ">
<div id= "myDiv "> </div>
这段代码可以实现动态设置newSelect变量的onFocus事件响应的函数;
但是换成下面的代码就不行了。
function addDib()
{ document.getElementById( "myDiv ").innerHTML=
" <select> <option> ddd </select> ";
document.getElementById( "myDiv ").firstChild.onFocus= "ee() ";
}
或者使用attachEvent方法也不行,这是为什么呢,应该如何解决?
各位高手,帮帮忙!
------解决方案--------------------function addDib()
{ document.getElementById( "myDiv ").innerHTML=
" <select> <option> ddd </select> ";
document.getElementById( "myDiv ").firstChild.onFocus= "ee() ";
}
改成
function addDib()
{ document.getElementById( "myDiv ").innerHTML=
" <select> <option> ddd </select> ";
document.getElementById( "myDiv ").firstChild.onfocus=ee;
}
具体原因我现在也不清楚,我去查查资料
------解决方案--------------------document.getElementById( "myDiv ").innerHTML=
" <select id= 'sel ' onFocus= 'ee(); '> <option> ddd </option> </select> ";
var a=document.all[document.getElementById( "myDiv ").firstChild.id];
改成它就能实现了!
原因不明,我也去查查!