[求助]大家来看下,为什么会获不到对象......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" target="_blank" >http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</A>"><BR><html xmlns="<a href="http://www.w3.org/1999/xhtml" target="_blank" >http://www.w3.org/1999/xhtml</A>"><BR><head><BR><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><BR><title>Test</title><BR><style type="text/css"><BR><!--<BR>body {<BR> margin-left: 0px;margin-top: 0px;}<BR>div{<BR>border:1px #CCCCCC solid; margin:100px; padding:5px; margin-top:100px}<BR>#menu{<BR>position:absolute; left:5px; height:400px; width:80px; font-family:"宋体"; font-size:12px; color:#2F3753;}<BR>#menu dl dt{<BR>list-style-type:none; text-align:center;}<BR>.dt{<BR>background:#999900;padding-top: 6px; padding-bottom: 5px; height:14px;}<BR>#other{<BR>position:absolute; left:180px; height:400px; width:600px;}<BR>--><BR></style><BR><script language="javascript"><BR><!--<BR>function init()<BR>{<BR> var nodeList = document.getElementsByTagName("dt");<BR> for (var i = 0; i < nodeList.length; i++)<BR> {<BR> function __event()<BR> {<BR> // alert(node);<BR> if(nodeList[i].className)<BR> {<BR> nodeList[i].className=''; <BR> }<BR> else<BR> {<BR> nodeList[i].className='dt';<BR> } <BR> } <BR> alert(nodeList[i]);<BR> nodeList[i].attachEvent("onmouseover",__event);<BR> nodeList[i].attachEvent("onmouseout",__event); <BR> } <BR>}<BR>--><BR></script><BR></head><BR><body onload="init();"><BR><div id="menu"><BR> <dl><BR> <dt>Title 1</dt><BR> <dt>Title 2</dt><BR> <dt>Title 3</dt><BR> </dl><BR></div><BR><div id="other"><img width="600px" height="400px" src="#" alt="other" /></div><BR></body><BR></html><BR>我想用JAVASCRIPT将所有DT标签附上ONMOUSEOVER等属性为什么会提示对象为空.....谁能帮我看看~! [flash=500,350]http://www.lanjing.com.cn/qiyeky/tt.swf[/flash] [flash=500,350]http://www.lanjing.com.cn/qiyeky/tt.swf[/flash] function init()<BR> {<BR> var nodeList = document.getElementsByTagName("dt");<BR> <BR> for (var i = 0; i < nodeList.length; i++)<BR> {<BR> _event(nodeList[i]);<BR> <BR> document.attachEvent("onmouseover",_event);<BR> document.attachEvent("onmouseout",_event);<BR> }<BR> }<BR> <BR> function _event(obj)<BR> {<BR> if(obj.className)<BR> {<BR> obj.className='';<BR> }<BR> else<BR> {<BR> obj.className='dt';<BR> }<BR> } lz和ls的都有错,正确的应该如下:<BR>function init()<BR> {<BR> var nodeList = document.getElementsByTagName("dt");<BR> <BR> for (var i = 0; i < nodeList.length; i++)<BR> {<BR> if (document.attachEvent) {<BR> document.attachEvent("onmouseover",_event);<BR> document.attachEvent("onmouseout",_event);<BR> } else if (document.addEventListener) {<BR> element.addEventListener("mouseover", _event,true);<BR> element.addEventListener("mouseout",_event,true);<BR> }<BR> }<BR> }<BR> <BR> function _event(event)<BR> {<BR> var e=window.event || event;<BR> var obj=e.target || e.srcElement;<BR> if(obj.className)<BR> {<BR> obj.className='';<BR> }<BR> else<BR> {<BR> obj.className='dt';<BR> }<BR> }<BR>}<BR><BR>如果lz嫌麻烦,并且知道prototype库的话,那么就这样用<BR>Event.observe(window,'load',function() {<BR> function _toggleClassName(e) {<BR> var obj=$(Event.element(e));<BR> if (obj.hasClassName('dt')) {<BR> obj.removeClassName('dt');<BR> } else {<BR> obj.addClassName('dt');<BR> }<BR> }<BR> $$('#menu dt').each(function(oDt) {<BR> oDt=$(oDt);<BR> oDt.observe(oDt,'mouseover',_toggleClassName.bindAsEventListener(oDt));<BR> oDt.observe(oDt,'mouseout',_toggleClassName.bindAsEventListener(oDt));<BR> });<BR>});页:
[1]
