帮忙看看这段JS为什么不能执行呢很简单·!高分~~~~~~~~~~~~~~~~~~急!~!!! 好像有效果呀,没有别的事件时没必要用attachEvent来添加事件,这样不能支持FF了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 event.srcElement 搞不懂为什么这样用我只知道用this 是没问题的。 <head><style type="text/css"> .btn_style { cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px; } </style></head><ul id="tt"> <li > guigblk </li> <li fdf="fdf2"> fdfef </li></ul><script> /** * 动态添加鼠标改变背景色事件 09/04/19 * */ function tarch(){ //event.srcElement.style.background = "red"; event.srcElement.className = "btn_style"; event.srcElement.style.background = "red"; } function tarch2(){ event.srcElement.style.background = ""; } var idd = document.getElementById("tt"); /* 获得当前子结点的长度 */ //var len = idd.childNodes.length; /* 循环添加鼠标事件 */ for(var i = 0;i < idd.getElementsByTagName('li').length; i++){ idd.getElementsByTagName('li')[i].attachEvent("onmouseover",tarch) idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2) } </script>表要用childNodes ,空格也会当成节点,切ie ff解释也是不一样的。 function tarch2(){ //event.srcElement.style.background = ""; event.srcElement.className = ""; //隐藏上面记录,跟tarch方法统一。 } 楼主的问题,按照我上面的方法就能解决了。。至于:idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2) idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2)如果你现在Internet Explorer浏览器下,调试时没有问题的。但到浏览器firefox(简称FF)下,运行的话,就不兼容了。 <head><style type="text/css"> .btn_style { cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px; } </style></head><ul id="tt"> <li> guigblk </li><li fdf="fdf2"><!--请注意<li>头接</li>尾--> fdfef </li></ul><script> /** * 动态添加鼠标改变背景色事件 09/04/19 * */ function tarch(){ //event.srcElement.style.background = "red"; event.srcElement.className = "btn_style"; } function tarch2(){ //event.srcElement.style.background = "";//请注意和tarch()对应 event.srcElement.className = ""; } var idd = document.getElementById("tt"); /* 获得当前子结点的长度 */ var len = idd.childNodes.length; /* 循环添加鼠标事件 */ for(var i = 0;i < len; i++){ idd.childNodes[i].attachEvent("onmouseover",tarch); idd.childNodes[i].attachEvent("onmouseout",tarch2); } </script>or:<head><style type="text/css"> .btn_style { cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px; } </style></head><ul id="tt"> <li> guigblk </li><li fdf="fdf2"><!--请注意<li>头接</li>尾--> fdfef </li></ul><script> /** * 动态添加鼠标改变背景色事件 09/04/19 * */ function tarch(){ event.srcElement.style.background = "red"; //event.srcElement.className = "btn_style"; } function tarch2(){ event.srcElement.style.background = "";//请注意和tarch()对应 //event.srcElement.className = ""; } var idd = document.getElementById("tt"); /* 获得当前子结点的长度 */ var len = idd.childNodes.length; /* 循环添加鼠标事件 */ for(var i = 0;i < len; i++){ idd.childNodes[i].attachEvent("onmouseover",tarch); idd.childNodes[i].attachEvent("onmouseout",tarch2); } </script> 如果事件列表中没有别的事件时可以直接使用obj.onmouseover=tarch;这样来添加事件否则,FF中可以obj.addEventListener("mouseover",tarch,false); <!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=utf-8" /><title>Untitled Document</title><style type="text/css">.btn_style { cursor:pointer;/*鼠标指针变手形用pointer,不要用hand,hand不是w3c的标准写法,只在IE下有用*/ width:84px; height:28px; background-color:red; pading:0px; border:0px;}.btn_style2 { cursor:pointer; width:84px; height:28px;background-color:; pading:0px; border:0px;}</style></head><body ><ul id="tt"> <li > guigblk </li> <li fdf="fdf2"> fdfef </li></ul><script> /** * 动态添加鼠标改变背景色事件 09/04/19 * */ function tarch(e){ var evn = e?e:event;//取得事件对象,兼容FF var el = evn.srcElement?evn.srcElement:evn.target;//取得事件的Element,srcElement为IE的,target为FF的 el.className = "btn_style"; } function tarch2(e){ var evn = e?e:event; var el = evn.srcElement?evn.srcElement:evn.target; el.className = "btn_style2"; } var idd = document.getElementById("tt").getElementsByTagName("li");//取得id为tt的ul下的所有的li /* 获得当前子结点的长度 */ var len = idd.length; /* 循环添加鼠标事件 */ for(var i = 0;i < len; i++){ if(document.attachEvent){//IE的添加事件 idd[i].attachEvent("onmouseover",tarch); idd[i].attachEvent("onmouseout",tarch2); }else{//FF的添加事件 idd[i].addEventListener("mouseover",tarch,false); idd[i].addEventListener("mouseout",tarch2,false); } } </script></body></html> 同意,attachEvent兼容性不好 我终于解决了困惑已久的样式问题,现求jquery里fadeout和fadein里的具体代码 如何做一个包含日期和时间的弹出层 ASP.NET 中关于递归的问题 大家有Ext JS源码分析与开发实例宝典 DVD么 string replace? javascript拖拽,不用document的onmousemove,而用div的。 这个js到底错在那里。 大伙请看,这网页怎么会这样??? 有没有DOM方面的资料呀 jquery如何实现根据选择框动态设置输入框的属性值? javascript 给TD添加子控件的问题,求救 用JavaScript如何实现图片定时更换
我只知道用this 是没问题的。
<style type="text/css">
.btn_style { cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px;
}
</style>
</head>
<ul id="tt">
<li >
guigblk </li>
<li fdf="fdf2">
fdfef
</li></ul><script>
/**
* 动态添加鼠标改变背景色事件 09/04/19
*
*/
function tarch(){
//event.srcElement.style.background = "red";
event.srcElement.className = "btn_style";
event.srcElement.style.background = "red";
}
function tarch2(){
event.srcElement.style.background = "";
} var idd = document.getElementById("tt"); /* 获得当前子结点的长度 */
//var len = idd.childNodes.length;
/* 循环添加鼠标事件 */
for(var i = 0;i < idd.getElementsByTagName('li').length; i++){
idd.getElementsByTagName('li')[i].attachEvent("onmouseover",tarch)
idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2)
} </script>表要用childNodes ,空格也会当成节点,切ie ff解释也是不一样的。
//event.srcElement.style.background = "";
event.srcElement.className = ""; //隐藏上面记录,跟tarch方法统一。 }
至于:
idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2)
idd.getElementsByTagName('li')[i].attachEvent("onmouseout",tarch2)
如果你现在Internet Explorer浏览器下,调试时没有问题的。
但到浏览器firefox(简称FF)下,运行的话,就不兼容了。
<head>
<style type="text/css">
.btn_style {
cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px;
}
</style>
</head>
<ul id="tt">
<li>
guigblk
</li><li fdf="fdf2"><!--请注意<li>头接</li>尾-->
fdfef
</li>
</ul><script>
/**
* 动态添加鼠标改变背景色事件 09/04/19
*
*/
function tarch(){
//event.srcElement.style.background = "red";
event.srcElement.className = "btn_style";
}
function tarch2(){
//event.srcElement.style.background = "";//请注意和tarch()对应
event.srcElement.className = "";
} var idd = document.getElementById("tt"); /* 获得当前子结点的长度 */
var len = idd.childNodes.length;
/* 循环添加鼠标事件 */
for(var i = 0;i < len; i++){
idd.childNodes[i].attachEvent("onmouseover",tarch);
idd.childNodes[i].attachEvent("onmouseout",tarch2);
} </script>or:<head>
<style type="text/css">
.btn_style {
cursor:hand; width:84px; height:28px; background-color:red; pading:0px; border:0px;
}
</style>
</head>
<ul id="tt">
<li>
guigblk
</li><li fdf="fdf2"><!--请注意<li>头接</li>尾-->
fdfef
</li>
</ul><script>
/**
* 动态添加鼠标改变背景色事件 09/04/19
*
*/
function tarch(){
event.srcElement.style.background = "red";
//event.srcElement.className = "btn_style";
}
function tarch2(){
event.srcElement.style.background = "";//请注意和tarch()对应
//event.srcElement.className = "";
} var idd = document.getElementById("tt"); /* 获得当前子结点的长度 */
var len = idd.childNodes.length;
/* 循环添加鼠标事件 */
for(var i = 0;i < len; i++){
idd.childNodes[i].attachEvent("onmouseover",tarch);
idd.childNodes[i].attachEvent("onmouseout",tarch2);
} </script>
否则,FF中可以obj.addEventListener("mouseover",tarch,false);
<!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=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.btn_style {
cursor:pointer;/*鼠标指针变手形用pointer,不要用hand,hand不是w3c的标准写法,只在IE下有用*/
width:84px;
height:28px;
background-color:red;
pading:0px;
border:0px;
}
.btn_style2 {
cursor:pointer;
width:84px;
height:28px;
background-color:;
pading:0px;
border:0px;
}
</style>
</head>
<body >
<ul id="tt">
<li > guigblk </li>
<li fdf="fdf2"> fdfef </li>
</ul>
<script>
/**
* 动态添加鼠标改变背景色事件 09/04/19
*
*/
function tarch(e){
var evn = e?e:event;//取得事件对象,兼容FF
var el = evn.srcElement?evn.srcElement:evn.target;//取得事件的Element,srcElement为IE的,target为FF的
el.className = "btn_style";
}
function tarch2(e){
var evn = e?e:event;
var el = evn.srcElement?evn.srcElement:evn.target;
el.className = "btn_style2";
} var idd = document.getElementById("tt").getElementsByTagName("li");//取得id为tt的ul下的所有的li /* 获得当前子结点的长度 */
var len = idd.length;
/* 循环添加鼠标事件 */
for(var i = 0;i < len; i++){
if(document.attachEvent){//IE的添加事件
idd[i].attachEvent("onmouseover",tarch);
idd[i].attachEvent("onmouseout",tarch2);
}else{//FF的添加事件
idd[i].addEventListener("mouseover",tarch,false);
idd[i].addEventListener("mouseout",tarch2,false);
}
} </script>
</body>
</html>
同意,attachEvent兼容性不好