问题
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