我想阻止选择特定的html元素,所以尝试应用" user-select" CSS属性可以在所有浏览器中实现。如下所示。
.section1{
-webkit-user-select: none; /* Chrome all / Safari all */
-moz-user-select: none; /* Firefox all */
-ms-user-select: none; /* IE 10+ */
user-select: none; /* Likely future */
}
当我应用" user-select" CSS属性为"无"对于所有"部分div"。但是当top(section1)和bottom(section3)之间的section(section2)没有应用属性时,当选择从section1(top)或section2(bottom)开始时,选择就会发生并延伸到第2节(中间)。
此问题仅在IE中发生,并且在chrome和firefox中也能正常工作。
封闭的JSFiddle链接相同。 https://jsfiddle.net/Thirunavukkarasu/bwf5emvp/
答案 0 :(得分:0)
对于IE< 10(如果用户选择不工作也是IE10)和Opera,您将需要使用您希望不可选择的元素的不可选属性。您可以使用HTML中的属性设置此项:
<div id="Div1" unselectable="on" class="unselectable">...</div>
Java脚本:
function makeUnselectable(node) {
if (node.nodeType == 1) {
node.setAttribute("unselectable", "on");
}
var child = node.firstChild;
while (child) {
makeUnselectable(child);
child = child.nextSibling;
}
}
makeUnselectable(的document.getElementById( “Div1构成”));