简体   繁体   English

如何使用 CSS 启用/禁用元素的点击处理程序?

[英]How can I enable/disable an element's click handler with CSS?

I am using it in JavaScript to enable and disable a div element:我在 JavaScript 中使用它来启用和禁用 div 元素:

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

But I want a property that enables and disables the element.但我想要一个启用和禁用元素的属性。 How can I do that?我怎样才能做到这一点?

html html

<div class="buttonSender" id="divbutton">Invia</div>

While I strongly recommend using a <button> instead of a <div> , I can think of one case where you might not be able to change the HTML markup to do that.虽然我强烈建议使用<button>而不是<div> ,但我可以想到一种情况,您可能无法更改 HTML 标记来做到这一点。
I start below with the case you should strive for, using a <button> for a button, but follow further below with how you can "disable" a div that is acting as a button.下面我从你应该努力争取的情况开始,使用<button>作为按钮,但在下面进一步说明如何“禁用”充当按钮的 div。


You can make a div act like a button by adding a click handler to it, then disable it simply by adding a class with the proper CSS, mainly by disabling pointer-events .您可以通过向其添加单击处理程序来使 div 像按钮一样,然后通过添加具有适当 CSS 的 class 来禁用它,主要是通过禁用pointer-events

Here, the <div> is acting as a button by using a class, and it gets disabled by adding another class, "disabled".在这里, <div>通过使用 class 充当按钮,并通过添加另一个 class 来禁用它,“禁用”。 The click handler on the div demonstrates it is clickable by using an alert , and you will see that it no longer reacts to clicks when the "disabled" class gets added to the div. div 上的点击处理程序通过使用alert来演示它是可点击的,当“禁用”的 class 添加到 div 时,您将看到它不再对点击做出反应。

 $('#divbutton').click(function(e) { // This is where you would put your code that // does something when the div is clicked. alert('The Fake Button was Clicked'); }); // This is how you can disable the fake button... $('#demo-disable').click(function(e) { $('#divbutton').addClass('disabled'); }); //...and re-enable it $('#demo-enable').click(function(e) { $('#divbutton').removeClass('disabled'); });
 div.buttonSender { margin: 1px; padding: 4px; border: 1px solid darkgray; border-radius: 2px; max-width: 20em; pointer-events: auto; color: black; background-color: peachpuff; } div.buttonSender.disabled { background-color: lightgray; pointer-events: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section> <div id="divbutton" class="buttonSender" role="button"> This is the div that is acting like a button.<br> Click Me </div> </section> <section class="demo-buttons"> <button id="demo-disable">Disable</button> <button id="demo-enable">Enable</button> </section>

I've added the role="button" on the div for the purposes of accessibility, but that is not all you would have to do for proper accessibility — see the " Note: " in the ARIA: button role documentation.出于可访问性的目的,我在 div 上添加了role="button" ,但这并不是您为获得适当的可访问性所要做的全部 - 请参阅ARIA: 按钮角色文档中的“注意: ”。

You would be much better off using a <button> instead of a <div> since you are able to put any HTML in the button tag that you could put in the div.使用<button>而不是<div>会更好,因为您可以将任何 HTML 放入可以放入 div 的按钮标签中。


The only reason (that I can think of) that you would "have to" use a div is if the HTML is written by someone else and you have no access to change it and no way to influence the author of the HTML.您“必须”使用 div 的唯一原因(我能想到的)是 HTML 是由其他人编写的,您无权更改它,也无法影响 HTML 的作者。
In that case you also aren't able to add classes or an ID, or write any new CSS, and would have to work with what is already there.在这种情况下,您也无法添加类或 ID,或编写任何新的 CSS,并且必须使用已经存在的内容。

This demo does that by modifying the CSS using jQuery's .css() method, disabling then restoring the pointer-events — note jQuery uses the camelCased property name, so it is pointerEvents not pointer-events .这个演示通过使用 jQuery 的.css()方法修改 CSS 来做到这一点,禁用然后恢复pointer-events - 注意 jQuery 使用 camelCased 属性名称,所以它是pointerEvents而不是pointer-events

 /* * This is NOT your code - this would be the click handler that already exists. */ $('#divbutton').click(function(e) { // Assume there is already a click handler, and you want to disable it. // This code would be the existing handler, somewhere else, not written by you. alert("Invia was clicked"); }); /* * This would be your code, and it wouldn't be packaged with the code above */ // This is how you can disable the fake button... $('#demo-disable').click(function(e) { $('#divbutton').css('pointerEvents', 'none'); }); //...and re-enable it $('#demo-enable').click(function(e) { $('#divbutton').css('pointerEvents', 'auto'); });
 #divbutton { border: 1px solid darkgray; border-radius: 2px; padding: 5px; max-width: 5em; } #demo-controls { margin-top: 2em; border-top: 1px solid gray; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section> <p> This area would be part of the page that you don't control.<br> The div acting like a button is below, and you can't change it. </p> <div class="buttonSender" id="divbutton">Invia</div> </section> <section id="demo-controls"> <p> This area would not be part of the HTML, over which you have no control, but somehow you need a way to fire your code that disables the existing div#divbutton <br> There needs to be <em>something</em> that fires your javascript; these buttons simulate that. </p> <button id="demo-disable">Disable</button> <button id="demo-enable">Enable</button> </section>

Try creating a CSS class with the value of pointer-events that you want like in the following example:尝试使用您想要的pointer-events值创建CSS class,如下例所示:

 document.querySelector('button').addEventListener('click', () => { document.getElementById('divbutton').classList.toggle('disabled'); });
 #divbutton.disabled { pointer-events: none; user-select: none; background:yellow; } #divbutton { height: 2rem; }
 <div class="buttonSender" id="divbutton">Invia</div> <button>click me</button>

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

相关问题 如何运行元素的click事件处理程序? - How can I run element's click event handler? 禁用并启用点击处理程序 - Disable and enable click handler 如何启用和禁用文档事件处理程序(具体点击) - How to enable and disable document event handler (specifically click) 如何使用 javascript 在 html 中安排启用/禁用元素? - How can I schedule enable/disable an element in html using javascript? 如何通过单击与jQuery相同的元素来启用和禁用滚动? - how can I enable and disable scroll by clicking the same element with jquery? 如何获取通过在 forEach 中添加的单击处理程序单击的元素? - How can I get the element that is clicked on with a click handler added in a forEach? 如何临时禁用元素的CSS悬停触发的动画? - How can I temporarily disable an element's CSS hover-triggered animation? 第一次单击处理程序会删除元素,而第二个处理程序不会运行,如何同时运行两者? - First click handler removes element and second handler doesn't run, how can I run both? 在JQuery UI Draggable元素的start事件处理程序中,如何使CSS位置更改生效? - How can I get a css position change to take effect during a JQuery UI Draggable element's start event handler? 如何在JavaScript中为img元素禁用/启用自定义单击事件 - How to disable/enable custom click event for an img element in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM