简体   繁体   English

停止触发.click()

[英]Stop .click() from firing

is there a way to stop .click() from firing when a condition has been met? 是否有办法在满足条件时停止触发.click()? My code looks like : 我的代码如下:

$(document).ready(function() {

// stocheaza ordinea corecta a imaginilor
var correctOrder = $("#puzzle > img").map(function() {
  return $(this).attr("src");
}).get();

// amesteca imaginile
var x = $("#puzzle > img").remove().toArray();
for (var i = x.length - 1; i >= 1; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var bi = x[i];
    var bj = x[j];
    x[i] = bj;
    x[j] = bi;
    }
$("#puzzle").append(x);

// event onclick pe 2 piese sa'si schimbe pozitia    
$("img.element").click(function(){
    if (clickCount == 0)
    {
        imgSrc = $(this).attr("src");
        lastImgId = $(this).attr("id");
        clickCount++;
    }
    else {
        $("#"+lastImgId).attr("src",$(this).attr("src"));
        $(this).attr("src",imgSrc);
        clickCount = 0;

// returneaza ordinea imaginilor curente
        var currentOrder = $("#puzzle > img").map(function() {
          return $(this).attr("src");
        }).get();

// compara ordinea imaginilor curente cu cea corecta
        if (currentOrder.compare(correctOrder)) 
        updateBubble(2);
    }
});
});

I need to stop $("img.element").click(function(){ } when this condition if (currentOrder.compare(correctOrder)) has been met. At the moment this .click() function allow me to click on 2 images and change their positions but updateBubble(2); is the final feedback and i must cannot change images when this feedback is given. Thx if (currentOrder.compare(correctOrder))这个条件,我需要停止$("img.element").click(function(){ } 。此刻,此.click()函数允许我点击2张图片并更改其位置,但updateBubble(2);是最终反馈,给出此反馈后,我必须不能更改图片。

Try this: 尝试这个:

 $("img.element").click(function(){
 if(currentOrder.compare(correctOrder))//return and do not execute click event code if condition
  return;

 //rest click event code here
 });

the general way to stop the click from firing is to use preventDefault, like this: 停止触发点击的一般方法是使用preventDefault,如下所示:

("img.element").click(function(event){
    if(condition) {
        event.preventDefault();
    }
}
$(this).unbind('click');

可能会满足您的需求。

Try 尝试

if(condition){
return false;
}

That will stop the other part of the function from firing! 这将阻止功能的其他部分触发!

Try this: 尝试这个:

 $("img.element").click(function(){
 if(currentOrder.compare(correctOrder) ){
   return false; // return and prevent further listener.
 }

if (clickCount == 0)
    {
        imgSrc = $(this).attr("src");
        lastImgId = $(this).attr("id");
        clickCount++;
    }
    else {
        $("#"+lastImgId).attr("src",$(this).attr("src"));
        $(this).attr("src",imgSrc);
        clickCount = 0;

// returneaza ordinea imaginilor curente
        var currentOrder = $("#puzzle > img").map(function() {
          return $(this).attr("src");
        }).get();

// compara ordinea imaginilor curente cu cea corecta
        if (currentOrder.compare(correctOrder)) 
        updateBubble(2);
    }


 });

I think you should change little bit like this with use of .on() to bind the event and .off() to unbind the event: 我想你应该改变有点像这样与使用.on()的事件和绑定.off()解除绑定的事件:

$("img.element").on('click', function(){ // use with .on() handler

and here to unbind the event: 并在此处取消绑定事件:

if (currentOrder.compare(correctOrder)) 
    updateBubble(2);
    $(this).off('click'); // use .off() to unbind the click event

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM