.d3{
width:60px;
height:60px;
border:1px solid red;
}
.d2{
width:40px;
height:40px;
border:1px solid red;
}
.d1{
width:20px;
height:20px;
border:1px solid red;
}
用户名:
$(".d1").bind("click",function(event){//event时间对象 当点击元素时 事件对象被创建 事件处理函数执行完后 事件对象被销毁
$(this).css("background","yellow");
event.stopPropagation();//停止事件冒泡 或者return false;
})
$(".d2").bind("click",function(event){
$(this).css("background","pink")
event.stopPropagation();
})
$(".d3").bind("click",function(event){
$(this).css("background","#000")
event.stopPropagation();
})
$(function(){
$("#sub").bind("click",function(event){
var username=$("#username").val();
if(username==""){
$("#msg").html("
文本框的值不能为空
");event.preventDefault();//阻止默认行为 或者用return false;
}
})
})
这段代码展示了如何在不同尺寸的div元素上绑定点击事件,并使用`event.stopPropagation()`来阻止事件冒泡,确保点击事件只在当前元素生效,不会影响到父元素。通过改变背景颜色来直观展示事件处理效果。
7779

被折叠的 条评论
为什么被折叠?



