注册 登录
编程论坛 jQuery论坛

[求助]大家来看下,为什么会获不到对象......

windtear 发布于 2007-06-17 10:24, 1645 次点击
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Test</title>
<style type="text/css">
<!--
body {
margin-left: 0px;margin-top: 0px;}
div{
border:1px #CCCCCC solid; margin:100px; padding:5px; margin-top:100px}
#menu{
position:absolute; left:5px; height:400px; width:80px; font-family:"宋体"; font-size:12px; color:#2F3753;}
#menu dl dt{
list-style-type:none; text-align:center;}
.dt{
background:#999900;padding-top: 6px; padding-bottom: 5px; height:14px;}
#other{
position:absolute; left:180px; height:400px; width:600px;}
-->
</style>
<script language="javascript">
<!--
function init()
{
var nodeList = document.getElementsByTagName("dt");
for (var i = 0; i < nodeList.length; i++)
{
function __event()
{
// alert(node);
if(nodeList[i].className)
{
nodeList[i].className='';
}
else
{
nodeList[i].className='dt';
}
}
alert(nodeList[i]);
nodeList[i].attachEvent("onmouseover",__event);
nodeList[i].attachEvent("onmouseout",__event);
}
}
-->
</script>
</head>
<body onload="init();">
<div id="menu">
<dl>
<dt>Title 1</dt>
<dt>Title 2</dt>
<dt>Title 3</dt>
</dl>
</div>
<div id="other"><img width="600px" height="400px" src="#" alt="other" /></div>
</body>
</html>
我想用JAVASCRIPT将所有DT标签附上ONMOUSEOVER等属性为什么会提示对象为空.....谁能帮我看看~!
4 回复
#2
melack2007-06-22 08:18
[flash=500,350]http://www.[/flash]
#3
melack2007-06-22 08:18
[flash=500,350]http://www.[/flash]
#4
advancant2007-10-05 23:42
function init()
{
var nodeList = document.getElementsByTagName("dt");

for (var i = 0; i < nodeList.length; i++)
{
_event(nodeList[i]);

document.attachEvent("onmouseover",_event);
document.attachEvent("onmouseout",_event);
}
}

function _event(obj)
{
if(obj.className)
{
obj.className='';
}
else
{
obj.className='dt';
}
}
#5
渚薰2007-10-08 10:13
lz和ls的都有错,正确的应该如下:
function init()
{
var nodeList = document.getElementsByTagName("dt");

for (var i = 0; i < nodeList.length; i++)
{
if (document.attachEvent) {
document.attachEvent("onmouseover",_event);
document.attachEvent("onmouseout",_event);
} else if (document.addEventListener) {
element.addEventListener("mouseover", _event,true);
element.addEventListener("mouseout",_event,true);
}
}
}

function _event(event)
{
var e=window.event || event;
var obj=e.target || e.srcElement;
if(obj.className)
{
obj.className='';
}
else
{
obj.className='dt';
}
}
}

如果lz嫌麻烦,并且知道prototype库的话,那么就这样用
Event.observe(window,'load',function() {
function _toggleClassName(e) {
var obj=$(Event.element(e));
if (obj.hasClassName('dt')) {
obj.removeClassName('dt');
} else {
obj.addClassName('dt');
}
}
$$('#menu dt').each(function(oDt) {
oDt=$(oDt);
oDt.observe(oDt,'mouseover',_toggleClassName.bindAsEventListener(oDt));
oDt.observe(oDt,'mouseout',_toggleClassName.bindAsEventListener(oDt));
});
});
1