【发布时间】:2014-11-11 03:17:36
【问题描述】:
我有这段代码。
点击按钮时必须按顺序显示红色、绿色和蓝色,但点击事件会直接传播到蓝色而不是绿色。当我使用e.stopproagation 时,jQuery 事件侦听器仍然附加到不存在的元素类。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Basket | Placelyrics</title>
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="index.css">
<script src="jquery.js"></script>
<script src="a.js"></script>
<script>
$(document).ready(function(){
$('.div1shown').click(function(event){
$('.red').hide();
$('.green').show();
$(this).removeClass('div1shown').addClass('div2shown');
console.log('a');
});
$('.container').delegate('.div2shown', 'click', function(event) {
$('.green').hide();
$('.blue').show();
$(this).removeClass('div2shown').addClass('div3shown');
console.log('b');
});
});
</script>
<style>
.box{
width:200px;
height:200px;
display: block;
}
.red{
background-color: red;
}
.green{
background-color: green;
display:none;
}
.blue{
background-color: blue;
display:none;
}
</style>
</head>
<body>
<div class="container">
<div class="box red">
</div>
<div class="box green">
</div>
<div class="box blue">
</div>
<button class="div1shown">submit</button>
</div>
</body>
</html>
【问题讨论】:
标签: javascript jquery event-handling onclicklistener jquery-events