Warm tip: This article is reproduced from serverfault.com, please click

HTML JS

发布于 2020-11-27 18:15:08

,hello Gentlemann please

How can i disable the click Behavior onclick through the css class please?

I can't use ID in the HTML Element because i'm not able to insert any ID in the code, so i need to use the Class the make the magic happen.

In all my Tries, none of them still works.

Element and CSS Class that i need to disable if clicked:

<label class="inner all disabled reserved my_numbers">
 <span class="p5_effect_tooltip_b top">
     <span class="numero">999999<br><small>pending<br>payment</small></span>
     <span class="p5_custom_tooltip_b">Pre-Reserved: Jim</span>
 </span>
</label>

Class: label.inner.all.disabled.reserved.my_numbers

1° First Try:

jQuery(document).ready(function() {
    $("#inner.all.disabled.reserved").click(function() {
        return false;
    });
});

2° Second Try:

$(document).ready(function() {
    $(".label.inner.all.disabled.reserved").click(function() {
        $("label").off("click");
    });
});

3° Third Try:

$(document).ready(function() {
    $("#inner.all.disabled.reserved").click(function() {
        return false;
    });
});

4° Try:

$('#inner.all.disabled.reserved.my_numbers').disabled = true
Questioner
Paulo do Porto
Viewed
0
Twisty 2020-11-28 12:15:26

I find it best to use the Event. You can use .preventDefault() to stop the event action.

Example:

$(function() {
  function preventClick(e) {
    e.preventDefault();
    console.log(e.target.nodeName + " Click Prevented");
    return false;
  }
  $("label.inner.all.disabled.reserved").click(preventClick).children().click(preventClick);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="inner all disabled reserved my_numbers">
 <span class="p5_effect_tooltip_b top">
     <span class="numero">999999<br><small>pending<br>payment</small></span>
     <span class="p5_custom_tooltip_b">Pre-Reserved: Jim</span>
 </span>
</label>