Javascript 停止。单击()以避免触发
是否有方法在满足条件时停止.click()触发?我的代码如下所示:Javascript 停止。单击()以避免触发,javascript,jquery,Javascript,Jquery,是否有方法在满足条件时停止.click()触发?我的代码如下所示: $(document).ready(function() { // stocheaza ordinea corecta a imaginilor var correctOrder = $("#puzzle > img").map(function() { return $(this).attr("src"); }).get(); // amesteca imaginile var x = $("#puzzle &g
$(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);
}
});
});
我需要停止$(“img.element”)。如果(currentOrder.compare(correctOrder))
满足此条件,请单击(function(){}。此时,此.click()函数允许我单击2个图像并更改其位置,但更新气泡(2)
是最终反馈,在给出此反馈时,我不能更改图像。Thx尝试以下操作:
$("img.element").click(function(){
if(currentOrder.compare(correctOrder))//return and do not execute click event code if condition
return;
//rest click event code here
});
$("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);
}
});
试试这个:
$("img.element").click(function(){
if(currentOrder.compare(correctOrder))//return and do not execute click event code if condition
return;
//rest click event code here
});
$("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);
}
});
可能会做你想做的事
可能会执行您正在查找的操作….阻止单击触发的一般方法是使用preventDefault,如下所示:
("img.element").click(function(event){
if(condition) {
event.preventDefault();
}
}
阻止单击触发的一般方法是使用preventDefault,如下所示:
("img.element").click(function(event){
if(condition) {
event.preventDefault();
}
}
试一试
这将阻止函数的其他部分启动!尝试
这将阻止函数的其他部分启动!尝试以下操作:
$("img.element").click(function(){
if(currentOrder.compare(correctOrder))//return and do not execute click event code if condition
return;
//rest click event code here
});
$("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);
}
});
试试这个:
$("img.element").click(function(){
if(currentOrder.compare(correctOrder))//return and do not execute click event code if condition
return;
//rest click event code here
});
$("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);
}
});
我认为您应该使用
.on()
绑定事件和.off()
解除绑定事件来进行如下更改:
$("img.element").on('click', function(){ // use with .on() handler
if (currentOrder.compare(correctOrder))
updateBubble(2);
$(this).off('click'); // use .off() to unbind the click event
在这里解开事件的束缚:
$("img.element").on('click', function(){ // use with .on() handler
if (currentOrder.compare(correctOrder))
updateBubble(2);
$(this).off('click'); // use .off() to unbind the click event
我认为您应该使用
.on()
绑定事件和.off()
解除绑定事件来进行如下更改:
$("img.element").on('click', function(){ // use with .on() handler
if (currentOrder.compare(correctOrder))
updateBubble(2);
$(this).off('click'); // use .off() to unbind the click event
在这里解开事件的束缚:
$("img.element").on('click', function(){ // use with .on() handler
if (currentOrder.compare(correctOrder))
updateBubble(2);
$(this).off('click'); // use .off() to unbind the click event
用条件包围整个单击处理程序!用条件包围整个单击处理程序!谁否决了这个…这正是你应该做的事谁否决了这个…这正是你应该做的事这只会阻止DOM树上更高的其他单击事件被激发。它不会被阻止t禁止触发函数中的其余代码。这只会防止触发DOM树更高级别的其他单击事件。这不会阻止函数中的其余代码触发。这看起来不错,但我无法从一开始就更改图像…我需要在多次更改图像后执行此块(这是一个混乱的谜题,我必须通过更改图像来发现整个图片)。当我发现整个图片时,我会得到更新气泡(2);反馈并停止触发单击此按钮看起来不错,但我无法从一开始就更改图像…我需要在多次更改图像后执行此块操作(这是一个混乱的谜题,我必须通过更改图像来发现整个图片)。当我发现整个图片时,我会得到更新气泡(2);反馈和停止触发单击此代码接近事实,但如果我强制单击,我仍然可以移动几块。已解决:$(“img.element”)。关闭(“单击”);是正确的形式,我真的很感谢你的帮助!也谢谢。很高兴来到这里!^ ^我有很多问题:现在我要做的是将拼图块拖放到右边,然后放到左边,我有其他与右边匹配的拼图块。问题是我的页面是可缩放的,如果我扔下拼图块,将会是一团乱麻。我有一个阅读关于offset()但我确定我需要帮助这段代码很接近事实,但如果我强制单击,我仍然可以移动几块。已解决:$(“img.element”).off(“单击”);是正确的形式,我真的很感谢你的帮助!也谢谢。很高兴来到这里!^ ^我有很多问题:现在我要做的是将拼图块拖放到右边,然后放到左边,我有其他与右边匹配的拼图块。问题是我的页面是可缩放的,如果我扔下拼图块,将会是一团乱麻。我有一个阅读关于offset()但我肯定我需要帮助