jQuery 将元素设置为不可点击,然后设置为可点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17462682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:08:36  来源:igfitidea点击:

Set element to unclickable and then to clickable

jquery

提问by IlludiumPu36

I want to have a div element set to not be clickable and then set it to clickable after another element is clicked.

我想将 div 元素设置为不可点击,然后在单击另一个元素后将其设置为可点击。

So, when .case is clicked:

所以,当 .case 被点击时:

$('.case').click(function() {  
$('#patient').addClass('active').on('click');
});

But how do I set #patient to unclickable in the first instance when I have further down:

但是,当我进一步向下时,如何在第一个实例中将 #patient 设置为不可点击:

$('#patient').click(function() { 
alert('clicked');
});

回答by kalley

$('#patient').on('click', function() {
    if ( $(this).hasClass('active') ) {
        // do whatever when it's active.
    }
    return false;
});

If you just want it to not accept, you can add this to your css (pointer-events):

如果您只是希望它不接受,您可以将其添加到您的 css ( pointer-events) 中:

#patient { pointer-events: none; }
#patient.active { pointer-events: auto; }

回答by Nalan Madheswaran

Non clickable:

不可点击:

$("#ElementName").css("pointer-events","none");

Clickable:

可点击:

$("#ElementName").css("pointer-events","auto");

回答by Mohammad Ismail Khan

$('#patient').unbind("click");

I hope this will work.

我希望这会奏效。

回答by sdespont

Depending on your need (Only one element or many, dynamic element creation or not) you can use :

根据您的需要(只有一个元素或多个,动态元素创建与否),您可以使用:

1) A variable to check if the first click on the other element has been done

1) 用于检查是否已完成对其他元素的第一次单击的变量

var isActive  = false;

$('.case').click(function() {  
    isActive = true;
});

$('#patient').click(function(){
    if(isActive === false)
        return false;

    //Your behaviour
});

2) Define the click function in the click event of the first element

2)在第一个元素的点击事件中定义点击函数

$('.case').on('click',function() { 
    //Make element clickable 
    $('#patient').on('click',function(){
        //Your behaviour
    });
});

Then you can use offto remove the click event

然后就可以使用off删除点击事件

回答by A.T.

for first instance..add deactive class to patient div tag...

$('#patient').click(function() { 
if($(this).hasClass('deactive'))
 return;
else
//do what you want to do.
});

on case click...

$('.case').click(function() {  
$('#patient').removeClass('deactive');
});

回答by Olrac

Use trigger

使用触发器

$('#patient').addClass('active').trigger('click');

REFERENCE : .trigger()

参考:.trigger()