当前位置: 代码迷 >> JavaScript >> setAttribute("onmouseover"的有关问题
  详细解决方案

setAttribute("onmouseover"的有关问题

热度:850   发布时间:2012-03-04 11:13:33.0
setAttribute("onmouseover",.....的问题
//代码如下
<div   id= "test "   style= "width:400px; ">
</div>
<script   language= "javascript "   type= "text/javascript ">
var   e1   =   document.getElementById( "test ");
var   newDiv   =   document.createElement( "div ");
var   textNode   =   document.createTextNode( "aaaaaaaaaaa ")
newDiv.setAttribute( "onmouseover ", "this.style.color= 'red ' ");
newDiv.setAttribute( "onmouseout ", "this.style.color= 'green ' ");
newDiv.appendChild(textNode);
e1.appendChild(newDiv);
</script>
//----------------------------------------------
//问题:当鼠标经过时没有反应
//而如下代码能正常运行
<div   onmouseover= "this.style.color= 'red ' "   onmouseout= "this.style.color= 'green ' ">
fsdfdsfsd
ssdfsdfsd
</div>

------解决方案--------------------
newDiv.onmouseover=new Function( "this.style.color= 'red ' ");
newDiv.onmouseout=new Function( "this.style.color= 'red ' ");
这样写行不?
  相关解决方案