先贴代码
<html>
<head>
<style>
.sltBox{border:1px solid #ccc; height:25px; margin:0; padding:0; width:50px;}
.sltBox ul,.sltBox ul li{ list-style:none; margin:0; padding:0;}
.sltBox ul{ position:absolute; line-height:25px; background:#999999;}
.sltBox ul li{display:block;}
</style> <title>仿照Select菜单</title></head>
<body onLoad="slt();">
<div id="sltBox" class="sltBox" onClick="if(test(this))alert('OK');">
<ul id="list" name="list">
<li>111111</li>
<li>222222</li>
<li>311111</li>
<li>411111</li>
</ul>
</div>
</body>
</html>
<script language="javascript">
function slt(){
var oBox=document.getElementById("sltBox");
var oList=oBox.children["list"];
oList.style.display="none";
oList.style.top=oBox.offsetTop+"px";
oList.style.left=oBox.offsetLeft+"px";
oList.onmouseout=function(){
if(test(oList,window.event))this.style.display="none";
}
oBox.onclick=function(){oList.style.display=oList.style.display?"":"none";};
}
function test(obj,e) {
if (e.currentTarget) {
if (e.currentTarget!= obj) {
return (obj != e.relatedTarget.parentNode)
}
} else {
if (e.toElement != obj) {
return (obj != e.toElement.parentNode)
}
}
}
</script>我想鼠标移开UL后,就隐藏UL里的所有内容,为了这个在网站找了好多代码.终于找到了,即那段test()函数,在IE6.0下通过,可在FF下就不行了.经firebug调试工具,发现window的event为undefine.郁闷.....
请高手指点一下.(分数不多,20分帮我解决一下吧)
<html>
<head>
<style>
.sltBox{border:1px solid #ccc; height:25px; margin:0; padding:0; width:50px;}
.sltBox ul,.sltBox ul li{ list-style:none; margin:0; padding:0;}
.sltBox ul{ position:absolute; line-height:25px; background:#999999;}
.sltBox ul li{display:block;}
</style> <title>仿照Select菜单</title></head>
<body onLoad="slt();">
<div id="sltBox" class="sltBox" onClick="if(test(this))alert('OK');">
<ul id="list" name="list">
<li>111111</li>
<li>222222</li>
<li>311111</li>
<li>411111</li>
</ul>
</div>
</body>
</html>
<script language="javascript">
function slt(){
var oBox=document.getElementById("sltBox");
var oList=oBox.children["list"];
oList.style.display="none";
oList.style.top=oBox.offsetTop+"px";
oList.style.left=oBox.offsetLeft+"px";
oList.onmouseout=function(){
if(test(oList,window.event))this.style.display="none";
}
oBox.onclick=function(){oList.style.display=oList.style.display?"":"none";};
}
function test(obj,e) {
if (e.currentTarget) {
if (e.currentTarget!= obj) {
return (obj != e.relatedTarget.parentNode)
}
} else {
if (e.toElement != obj) {
return (obj != e.toElement.parentNode)
}
}
}
</script>我想鼠标移开UL后,就隐藏UL里的所有内容,为了这个在网站找了好多代码.终于找到了,即那段test()函数,在IE6.0下通过,可在FF下就不行了.经firebug调试工具,发现window的event为undefine.郁闷.....
请高手指点一下.(分数不多,20分帮我解决一下吧)
这里该下
e = e || window.event;
if (e.currentTarget) {
if (e.currentTarget!= obj) {
return (obj != e.relatedTarget.parentNode)
}
} else {
if (e.toElement != obj) {
return (obj != e.toElement.parentNode)
}
}
//首先,定义一个全局的event
if( typeof(window.event)=="undefined" ){
eval("var event = new Object;");
}
// Firefox Event << IE Event
// bind ie's methods on firefox
function jskitFFEvent(e){
if( typeof(document.all)=="undefined" ){
event = e;
event.srcElement = e.target;
}
return true;
};
//然后在绑定事件的地方:
if ( typeof(document.all)=="undefined" ) {//for Firefox
var _eventName = rName.replace(/on(.*)/i,'$1');
eval("_bk = rObj.addEventListener(\""+_eventName+"\", function(){jskitFFEvent(event);"+rHandler+"();}, true);");
} else{//for IE
eval("_bk = rObj.attachEvent(\""+rName+"\","+ rHandler+");");
}
//这里rHandler是传递过来的方法名
//调用的方式形如:
jskitEvents.add(myElement,"onclick","myClass.onclick");
//该方式还可以给绑定事件传递参数,只不过暂时我还没有用到,所以没写那么多。 //首先,定义一个全局的event
if( typeof(window.event)=="undefined" ){
eval("var event = new Object;");
}
// Firefox Event << IE Event
// bind ie's methods on firefox
function jskitFFEvent(e){
if( typeof(document.all)=="undefined" ){
event = e;
event.srcElement = e.target;
}
return true;
};//然后在绑定事件的地方:
if ( typeof(document.all)=="undefined" ) {//for Firefox
var _eventName = rName.replace(/on(.*)/i,'$1');
eval("_bk = rObj.addEventListener(\""+_eventName+"\", function(){jskitFFEvent(event);"+rHandler+"();}, true);");
} else{//for IE
eval("_bk = rObj.attachEvent(\""+rName+"\","+ rHandler+");");
}
//这里rHandler是传递过来的方法名
//调用的方式形如:
jskitEvents.add(myElement,"onclick","myClass.onclick");
//该方式还可以给绑定事件传递参数,只不过暂时我还没有用到,所以没写那么多。
使用了这样的方式之后,以前在函数内用的event.srcElement都不想要在改动了。我想类似的方式也能解决其他firefox与ie兼容的问题吧,把对象重构,把不兼容的地方抽出来之前解决,做一个对应的转换,以后的代码都不需要再做修改或者不用每次都要做两态判断的处理(后一种情况让我非常头疼)。 我现在做的工作是不要在页面中(主要是标签中)写任何js代码,所有的js脚本都通过配置和部署来应用,让js自己来判断该去绑定谁,该去执行什么,页面只负责src的引用。 JavaEye我是刚刚才知道的,看文章的时候看着看着就蹦出来一个不知道的新名词。唉,为什么老是出来新东西来残害老年人的智商。
<head>
<style>
.sltBox{border:1px solid #ccc; height:25px; margin:0; padding:0; width:50px;}
.sltBox ul,.sltBox ul li{ list-style:none; margin:0; padding:0;}
.sltBox ul{ position:absolute; line-height:25px; background:#999999;}
.sltBox ul li{display:block;}
</style> <title>仿照Select菜单</title></head>
<body onLoad="slt();">
<div id="sltBox" class="sltBox" onClick="if(test(this))alert('OK');">
<ul id="list" name="list">
<li>111111</li>
<li>222222</li>
<li>311111</li>
<li>411111</li>
</ul>
</div></body>
</html>
<script language="javascript">
function slt(){
var oBox=document.getElementById("sltBox");
var oList=oBox.children["list"];
oList.style.display="none";
oList.style.top=oBox.offsetTop+"px";
oList.style.left=oBox.offsetLeft+"px";
oList.onmouseout=function(event){
if(test(oList,event))this.style.display="none";
}oBox.onclick=function(){oList.style.display=oList.style.display?"":"none";};
}
function test(obj,e) {
if (e.currentTarget) {
if (e.currentTarget!= obj) {
return (obj != e.relatedTarget.parentNode)
}
} else {
if (e.toElement != obj) {
return (obj != e.toElement.parentNode)
}
}
}
</script>
所以你处理事件的时候需要分别判断,所以你的代码只要修改为以下即可:
oList.onmouseout=function(event){
if(test(oList,event||window.event))this.style.display="none";
}
#2楼 由于我对JS研究不深,没有看懂代码原理。
#3楼 这样一改,我IE,FF都有问题了。
#4楼 调试出来,问题跟原来一样。
http://youth.huanghuai.edu.cn/do/bencandy.php?fid=45&id=4354
ff等标准浏览器下,会将event对象作为事件处理函数的第一个参数传进去,而ie下event是作为window对象的一个属性存在的。
aa.onclick = function(){
//ff下arguments[0]就是event对象
}
不过通常我们会这样写
aa.onclick = function(e){
//如果是在ff下,那么变量e就是event对象,但是对于ie来说e是undefined,于是前辈们想到了下面的方法
e = e||event;//即如果在ff下,e就是event,还将e赋值给e本身。如果在ie下,e是undefined,所以就会被赋值为window.event。当然window可以省略不写。
}
Javascript在IE和FireFox中的不同表现