Javascript 如何检查嵌套事件是否触发?
在本例中,我想首先触发swiperight函数,并检查书本是否“按下”。如何将bind“pressed”转换为条件语句?多谢各位Javascript 如何检查嵌套事件是否触发?,javascript,jquery,jquery-mobile,Javascript,Jquery,Jquery Mobile,在本例中,我想首先触发swiperight函数,并检查书本是否“按下”。如何将bind“pressed”转换为条件语句?多谢各位 $(document).swiperight(function(){ $("#book").bind("pressed", function(event) { // Change this statement to if ($("#book") is "pressed"){..} console.log("press
$(document).swiperight(function(){
$("#book").bind("pressed", function(event) { // Change this statement to if ($("#book") is "pressed"){..}
console.log("pressed");
});
});
没有本机的“按下”事件,所以我猜这是您自己触发的自定义事件,使用$(“#book”).trigger(“按下”)
。如果是这种情况,则在触发事件之前添加一个标志:
$("#book").data("pressed", "true")
$("#book").trigger("pressed")
然后,无论何时您想要检查:
if ($("#book").data("pressed") === "true") {
//already pressed...
}
试一试
什么是书?按下
也是一个属性还是什么?book是一个div,按下是一个#book的事件book
的事件?您是否正在尝试检查是否已经有绑定到该事件的函数?确定,但根据某些条件发出。请查看这些函数是什么,并使用它们。因此,当按下时,在该事件中跟踪状态。有什么问题吗?我写了同样的东西(不是“点击”,而是“按下”,假装“按下”是自定义事件)。不管怎么说,我想它还需要一个控件来跟踪“#book”何时“未压缩”。我不是投反对票的人。是的,我明白了。我想做的事情只有在书还没出版的时候我才做。我已经添加了切换按钮式的行为。如果单击书本,它将被标记为已按下,如果再次单击,它将被取消标记。
$("#book").click(function(event) {
var $this = $(this);
//emulate toggle button
$this.data("pressed", $this.data("pressed") ? '' : 1);
});
$(document).swiperight(function(){
if(!$("#book").data("pressed")) { //If not pressed
//your code goes here
}
});
$(document).swiperight(function(){
if($("#book").attr("pressed") === "true") {
$("#book").bind("pressed", function(event) {
$(this).attr("pressed", "true");
console.log("pressed");
});
}
});