简体   繁体   English

IE8文本选择问题

[英]IE8 text selection issue

IE8 text selection issue. IE8文本选择问题。

I have used jquery ui and created a resizeable div. 我已经使用了jQuery ui并创建了一个可调整大小的div。 So basically when i resize the div text also gets selected and also if i need to resize it again i need to click outside of the LI and then resize it again. 因此,基本上,当我调整div文本的大小时,也将被选中,并且如果我需要再次调整它的大小,则需要在LI外部单击,然后再次调整它的大小。

I've tried 我试过了

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

-webkit-user-drag: none; 
-moz-user-drag: none; 
user-drag: none;

Also tried few js codesnippets as well but could not get it fixed. 还尝试了一些js代码片段,但无法将其修复。

My fiddle : http://jsfiddle.net/svXTa/16/ 我的小提琴: http : //jsfiddle.net/svXTa/16/

Any help? 有什么帮助吗?

Below is the image of the issue. 下面是问题的图像。

在此处输入图片说明

Use 采用

$('ul, #dgArea').disableSelection();

This will only disable selections where the initial mouse press originates in one of those elements. 这只会禁用最初按下鼠标时来自这些元素之一的选择。 If the mouse press begins elsewhere the text could still get highlighted. 如果在其他地方开始按下鼠标,则文本仍会突出显示。

If you don't care about people being able to highlight the text in your container you could set it at .container level. 如果您不关心人们是否可以突出显示容器中的文本,则可以将其设置为.container级别。

The ideal solution is to use the CSS property user-select to disable text selection, along with all its vendor prefixes: 理想的解决方案是使用CSS属性user-select禁用文本选择及其所有供应商前缀:

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

Unfortunately the CSS solution is not supported in IE8, so you still need to use Javascript. 不幸的是,IE8不支持CSS解决方案,因此您仍然需要使用Javascript。

As Brandon suggested, jQuery UI's disableSelection() is one option, but it may have some undesirable side-effects: if you look at the jQuery UI source code , the function works by short-circuiting the onselectstart event if present, otherwise it will cancel onmousedown . 正如布兰登建议的那样,jQuery UI的disableSelection()是一个选项,但它可能会产生一些不良影响:如果您查看jQuery UI源代码 ,则该函数通过短路onselectstart事件(如果存在)来工作,否则它将取消onmousedown Since onselectstart is nonstandard and not supported in Firefox, this would disable clicking in that browser, which is probably not desirable. 由于onselectstart是非标准的,并且在Firefox中不受支持,因此这将禁用在该浏览器中的单击,这可能是不希望的。

The best solution is a combination of the above CSS and some Javascript to disable onselectstart and only onselectstart with something like this: 最好的解决办法是上述CSS和一些JavaScript的组合禁用onselectstart ,只有onselectstart像这样的东西:

$("ul, #dgArea").on("selectstart", function(e) { e.preventDefault(); });

CSS alone can't do the job. 单靠CSS不能完成这项工作。 here is an IE8 specific pure JS solution that works nicely for me : 这是一个特定于IE8的纯JS解决方案,对我来说效果很好:

// test if is an old browser with lazyload optimisation
// because I often need to test if it is IE8
function isIE8(){
    var rv = -1; // Return value assumes failure.
    var appName = navigator.appName.toLowerCase();

    if (appName.indexOf('nternet')*appName.indexOf('xplorer') > 1) {
        var ua = navigator.userAgent.toLowerCase();
        var re = new RegExp("msie ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua))
            rv = parseFloat(RegExp.$1);
    }
    // LazyLoad optimisation
    isIE8 = function(){return (rv == 8);};
    return isIE8(); // 2 : call
}

function disableSelection(anElt){
// double check because some browsers mess with navigator.appName and userAgent
if(isIE8 && anElt.attachEvent) 
    anElt.attachEvent("onselectstart", function(){return false;});
}

voilà :) voilà:)

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

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