disabling text selection is not working in IE using jquery

◇◆丶佛笑我妖孽 提交于 2019-12-08 11:06:32

问题


I tried disabling text selection using JQuery. It's working fine in Firefox but not in IE. I have a text input field with an id, say 123. For this element the text filled in should not be allowed for selection. Can anyone provide me with sample code? I am using IE 6.


回答1:


Try this: I don't have IE6, but this works for IE7 at least

   $("#123").bind( "selectstart", function(e){
    e.preventDefault();
    return false;
    });



回答2:


Take a look one of IE's proprietary, cancellable events, onselectstart.

$("#123").bind("selectstart", function (evt) {
    evt.preventDefault();
});

Working example (requires IE 6-10): http://jsfiddle.net/AndyE/Cp76b/

Combine this handler with your current solution. Note that some browsers can disable user selection via the CSS3 user-select property or the unselectable (IE, Opera only) attribute. For example:

<input type="text" id="123" unselectable="on">
#123 { 
    -webkit-user-select: none; 
    -moz-user-select: none;
    -ms-user-select: none;
}

Working example updated: http://jsfiddle.net/AndyE/Cp76b/2

This makes the JavaScript solution redundant in those browsers, but gives the advantage of disabling the selection for those users that have JS disabled.



来源:https://stackoverflow.com/questions/8038010/disabling-text-selection-is-not-working-in-ie-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!