How to make a Ul list item unselectable

This question already has an answer here:

  • How to disable text selection highlighting? 37 answers

  • It does work, but I think the problem you might be having is in the way you are setting up your HTML.

    Ensure that the <li> elements you want to not be selectable have the unselectable class, as per this example:

    <li class="unselectable">unselectable</li>
    

    Fiddle:
    http://jsfiddle.net/TtLQa/1/

    Also, please refer to this link for browser support information with regards to user-select:none .


    Edit:

    I just saw your comment, you want to do this via javascript.

    Using jQuery, you can easily add or remove a class as you wish:

    $(element).addClass("unselectable");
    $(element).removeClass("unselectable");
    
    //removes it if it is there, or adds it if it is not
    $(element).toggleClass("unselectable"); 
    

    Fiddle:
    http://jsfiddle.net/TtLQa/4/

    链接地址: http://www.djcxy.com/p/7126.html

    上一篇: 文字选择在Web应用程序中的最佳实践

    下一篇: 如何使U1列表项无法选择