5 回答

TA贡献1895条经验 获得超3个赞
$(".num-btn").on("touchstart","span",function(event){
var target = $(event.target);
if(target.hasClass("icon-one")){
layer.open({
title:'请说出保单号',
shadeClose: false,
style: 'width:5rem;border:none;background:linear-gradient(to right bottom,#f14f63,#7889fb);color:#fff;',
content:$("#saying").html(),
btn:['确认'],
yes:function(index){
layer.close(index)
},
})
}else if(target.hasClass("icon-two")){
layer.open({
title:'请说出身份证号',
shadeClose: false,
style: 'width:5rem;border:none;background:linear-gradient(to right bottom,#f14f63,#7889fb);color:#fff;',
content:$("#saying").html(),
btn:'确认提交',
yes:function(index){
layer.close(index)
},
})
}
})

TA贡献1890条经验 获得超9个赞
具体逻辑没看,但if($(".icon-one"))
这种写法肯定是不对的,因为$(".icon-one")
返回的是个jQ对象,自动转换为true
,这样上边的分支实际就变成常通了。
这种一般是要在事件回调中用到$(this)
,这个是jQ对e.target
的一个封装(相当于$(e.target)
),它返回的是jQ封装的事件被触发的那个DOM对象,而判断是否包含一个类,则可以用.is()
这个API。简而言之,写成if ( $(this).children().is(".icon-one") )
吧。

TA贡献2080条经验 获得超4个赞
随便写一个吧。也不知道这样是不是你的意思:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button class="button A">button A</button>
<button class="button B">button B</button>
<button class="button C">button C</button>
<script src="jquery.js"></script>
<script>
$('body').on('click', '.button', function () {
var _this = $(this);
if (_this.hasClass('A')) {
alert('click A');
} else if (_this.hasClass('B')) {
alert('click B');
} else if (_this.hasClass('C')) {
alert('click C');
}
});
</script>
</body>
</html>
添加回答
举报