我有以下ul。我想让任何特定的列表项无法选择。
<ul>
<li>ABC</li>
<li>PQR</li>
<li>XYZ</li>
</ul>
我该怎么做?我尝试设置以下css类,但没有帮助
.unselectable {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
user-select: none
}
答案 0 :(得分:4)
它确实有效,但我认为您可能遇到的问题与设置HTML的方式有关。
确保您不想选择的<li>
元素具有unselectable
类,如下例所示:
<li class="unselectable">unselectable</li>
<强>小提琴:强>
http://jsfiddle.net/TtLQa/1/
另外,有关user-select:none
的浏览器支持信息,请参阅this link。
修改强>
我刚看到你的评论,你想通过javascript做到这一点。
使用jQuery,您可以根据需要轻松添加或删除类:
$(element).addClass("unselectable");
$(element).removeClass("unselectable");
//removes it if it is there, or adds it if it is not
$(element).toggleClass("unselectable");
<强>小提琴:强>
http://jsfiddle.net/TtLQa/4/