Making an element unselectable using jQuery

前端 未结 4 2173
生来不讨喜
生来不讨喜 2021-02-10 09:35

What\'s the best way to make an element unselectable using jQuery?

I know you can use onselectstart=\"return false;\" ondragstart=\"return false;\" in the H

相关标签:
4条回答
  • 2021-02-10 10:08

    $('.noselect').live('selectstart dragstart', function(evt){ evt.preventDefault(); return false; });

    Just bind the event to a live, and return false, or prevent default.

    0 讨论(0)
  • 2021-02-10 10:11

    I believe this is a better answer.

    $('*[data-noselect="true"]').focus(
        function () {
            $(this).blur();
        });
    
    $('*[data-noselect="true"] *').focus(
        function () {
            $(this).blur();
        });
    

    Then you can just add a data-noselect="true" to any element.

    0 讨论(0)
  • 2021-02-10 10:16

    I think jQuery will abstract the browser variance & so it will help you make HTML elements unselectable.

    Having said that, please note that the client can decide to switch off javascript, in which case you need to ensure that he still cannot do anything that will cause harm...

    HTH.

    0 讨论(0)
  • 2021-02-10 10:18

    If you use JqueryUI you can set class="ui-state-default" to the div that you dont want to be selected.

    <div class="ui-state-default"></div>
    
    0 讨论(0)
提交回复
热议问题