<div id="switch-button" class="switch">
<div class="switchOn"></div>
</div>
<script>
$(document).ready(function(){
//
$("body").on("click",".switch",function(e){
e.stopPropagation();
console.log(this);
var length=$(this).find(".switchOn").length;
console.log(length);
if(length>0){
$(this).toggleClass("switchOn",function(){
alert("使用回调函数,先隐藏再弹出对话框!");
})
}
})
});
//回调
</script>
你调用的这个版本的toggleClass(字符串)没有回调配置啊,直接看api toggleClass参数是什么的,不能乱传
/* $(this).toggleClass("switchOn", function () {
alert("使用回调函数,先隐藏再弹出对话框!");
})*/
//这样才是回调的版本
$(this).toggleClass(function (index, className) {
alert("使用回调函数,先隐藏再弹出对话框!");
if ($(this).hasClass('switchOn')) { $(this).removeClass('switchOn'); return '' }
return 'switchOn'
})
1.你既然用on绑定点击事件,就不用写在ready里面。
2.$(document).on("click",".switch",function(){}) 这样写
class为switch和switchOn的div宽高设置下,大小为0,怎么触发点击事件呀!
是你的代码没有触发回调吧