每个div鼠标经过时div背景颜色变化,但不想让镶嵌的div的父div变色,如何做?
听说是什么冒泡事件,不懂。demo:
http://skyming.13.bname.us/question/div_hover.htm
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function div_hover(){
$("div").hover(
function(){
$(this).addClass("hover");
},
function(){
$(this).removeClass("hover");
}
);
}
$(function(){
div_hover();
});
</script>
<style type="text/css">
.box1{background:green;width:400px;height:400px;}
.box2{background:yellow;width:300px;height:300px;}
.box3{background:#cc3333;width:200px;height:200px;}
.hover{background:#33cc33}
</style>
<div class="box1">
<div class="box2">
<div class="box3"></div>
</div>
</div>
听说是什么冒泡事件,不懂。demo:
http://skyming.13.bname.us/question/div_hover.htm
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function div_hover(){
$("div").hover(
function(){
$(this).addClass("hover");
},
function(){
$(this).removeClass("hover");
}
);
}
$(function(){
div_hover();
});
</script>
<style type="text/css">
.box1{background:green;width:400px;height:400px;}
.box2{background:yellow;width:300px;height:300px;}
.box3{background:#cc3333;width:200px;height:200px;}
.hover{background:#33cc33}
</style>
<div class="box1">
<div class="box2">
<div class="box3"></div>
</div>
</div>
$("div").hover(
function(e){
e.stopPropagation();
$(this).addClass("hover");
},
function(e){
e.stopPropagation();
$(this).removeClass("hover");
}
);
}
<!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>test</title>
<style type="text/css">
.box1{background:green;width:400px;height:400px;}
.box2{background:yellow;width:300px;height:300px;}
.box3{background:#cc3333;width:200px;height:200px;}
.hover{background:#33cc33}
</style>
<script src="jquery-1.3.2.js"></script>
<script>
$(function(){
$("div").toggle(
function(e){
e.stopPropagation();
$(this).addClass("hover");
},
function(e){
e.stopPropagation();
$(this).removeClass("hover");
}
);
});
</script></head><body>
<div class="box1">111111
<div class="box2">222222
<div class="box3">3333333</div>
</div>
</div>
</body>
</html>
hover事件不好测,用点击比较好测
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function div_hover(){
var levels = {};
var min = 100, max = 0;
var change = function() {
var q = 0;
for (var p in levels) {
$(levels[p]).removeClass("hover");
q = Math.max(q, p);
}
$(levels[q]).addClass("hover");
};
var getLevel = function(element) {
var level = 0;
for (var parent = element; parent.parentNode; parent = parent.parentNode) level++;
return level;
};
$("div").hover(
function(){
levels[getLevel(this)] = this;
change();
},
function(){
delete levels[getLevel(this)];
$(this).removeClass("hover");
change();
}
);
}
$(function(){
div_hover();
});
</script>
<style type="text/css">
.box1{background:green;width:400px;height:400px;}
.box2{background:yellow;width:300px;height:300px;}
.box3{background:#c33;width:200px;height:200px;}
.box4{background:#f0f;width:100px;height:100px;}
.hover{background:#33cc33}
</style>
<div class="box1">
<div class="box2">
<div class="box3">
<div class="box4"></div>
</div>
</div>
</div>
不知道还有没有更好的方法
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function div_hover(){
$("div").hover(
function(){
$(this).addClass("hover");
},
function(){
$(this).removeClass("hover");
}
);
}
$(function(){
div_hover();
});
</script>
<style type="text/css">
.box1{background:green;width:400px;height:400px;position:absolute;left:0;top:0;}
.box2{background:yellow;width:300px;height:300px;position:absolute;left:0;top:0;}
.box3{background:#c33;width:200px;height:200px;position:absolute;left:0;top:0;}
.box4{background:#f0f;width:100px;height:100px;position:absolute;left:0;top:0;}
.hover{background:#33cc33}
</style>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
event.stopPropagation();
//blah blah
});
<script type="text/javascript">
function div_hover(){
$("div").hover(
function(){
$(this).addClass("hover");
if(window.event){
window.event.cancelBubble = true;
}else{
oEvent.stopPropagation();
}
},
function(){
$(this).removeClass("hover");
}
);
}
$(function(){
div_hover();
});
</script>
<style type="text/css">
.box1{background:green;width:400px;height:400px;}
.box2{background:yellow;width:300px;height:300px;}
.box3{background:#cc3333;width:200px;height:200px;}
.hover{background:#33cc33}
</style>
<div class="box1">
<div class="box2">
<div class="box3"></div>
</div>
</div>