简体   繁体   English

IE中的文本选择预防问题

[英]Issue with text selection prevention in IE

I want to prevent selection for a particular html element, so tried applying the "user-select" CSS property to make this happen across all the browsers. 我想阻止选择特定的html元素,因此尝试应用“user-select”CSS属性以在所有浏览器中实现这一点。 something like below. 类似下面的东西。

    .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 */    
    }

This works fine when I applied "user-select" CSS property as "none" for all the "sections div". 当我为所有“sections div”应用“user-select”CSS属性为“none”时,这很好用。 But when the section (section2) in between the top (section1) and the bottom (section3) doesn't have the property applied, and selection is happening when the selection starts from either from section1(top) or section2(bottom) and extends till section 2(midddle). 但是当top(section1)和bottom(section3)之间的section(section2)没有应用属性时,当选择从section1(top)或section2(bottom)开始时选择发生并且扩展直到第2节(中段)。

This issue happens only in IE and the same works fine in chrome and firefox. 此问题仅在IE中发生,并且在chrome和firefox中同样可以正常工作。

Enclosed JSFiddle link for the same. 封闭的JSFiddle链接也是如此。 https://jsfiddle.net/Thirunavukkarasu/bwf5emvp/ https://jsfiddle.net/Thirunavukkarasu/bwf5emvp/

For IE < 10 (also IE10 if user-select not working ) and Opera, you will need to use the unselectable attribute of the element you wish to be unselectable. 对于IE <10(如果用户选择不工作也是IE10)和Opera,您将需要使用您希望不可选择的元素的不可选属性。 You can set this using an attribute in HTML: 您可以使用HTML中的属性进行设置:

<div id="Div1" unselectable="on" class="unselectable">...</div>

Java Script : 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")); makeUnselectable(的document.getElementById( “Div1构成”));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM