This question already has an answer here:
这个问题已经有了答案:
- Prevent text selection after double click 8 answers
- 在双击8个答案后,防止文本选择。
I have this jQuery toggle. It work fine.
我有这个jQuery切换。这工作很好。
<ul>
<li>Go to the store</li>
<li>Pick up dinner</li>
<li>Debug crash</li>
<li>Take a jog</li>
</ul>
$("li").toggle(
function () {
$(this).css({"list-style-type":"disc", "color":"blue"});
},
function () {
$(this).css({"list-style-type":"disc", "color":"red"});
},
function () {
$(this).css({"list-style-type":"", "color":""});
}
);
The problem is when I do fast clicking, it highlighted the text in it. Is there a way to stop the text from being highlighted?
问题是当我快速点击时,它会突出显示其中的文本。有没有办法阻止文本被高亮显示?
9 个解决方案
#1
52
I'm writing on iPhone, while away from the desk, but a quick Google turned up this page: disable text selection with jQuery.
我是在iPhone上写的,虽然离开了桌面,但是一个快速的谷歌出现了这个页面:用jQuery禁用文本选择。
Edited in response to the 'dead link' comment (from @Herb Caudill). While the original link is, indeed, dead, it appears to be due to a site restructuring (rather than removal) and the new location for the article can be found here: http://chris-barr.com/index.php/entry/disable_text_selection_with_jquery/
编辑回应“死链接”评论(来自@Herb Caudill)。虽然最初的链接确实已死,但它似乎是由于站点重组(而不是删除),本文的新位置可以在这里找到:http://chris-barr.com/index.php/entry/disable_text_selection_with_jquery/
And the code provided in that article is reproduced below:
该条所载的守则如下:
$(function(){
$.extend($.fn.disableTextSelect = function() {
return this.each(function(){
if($.browser.mozilla){//Firefox
$(this).css('MozUserSelect','none');
}else if($.browser.msie){//IE
$(this).bind('selectstart',function(){return false;});
}else{//Opera, etc.
$(this).mousedown(function(){return false;});
}
});
});
$('.noSelect').disableTextSelect();//No text selection on elements with a class of 'noSelect'
});
jQuery snippet written by Chris Barr, of chris-barr.com, as accessed on Friday, 21st of January, 2011.
#2
35
If you use jQuery UI you can disable text selection as simple as that:
如果您使用jQuery UI,您可以禁用如下简单的文本选择:
$("body").disableSelection();
#3
33
I solved this using the non-standard CSS keyword user-select:
我用非标准的CSS关键字用户选择解决了这个问题:
.unselectable {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
#4
10
//function to be reused later
function clearSelection() {
var sel ;
if(document.selection && document.selection.empty){
document.selection.empty() ;
} else if(window.getSelection) {
sel=window.getSelection();
if(sel && sel.removeAllRanges)
sel.removeAllRanges() ;
}
}
$('p').click(clearSelection);
源
#5
6
I had the same problem and this worked for me:
我遇到了同样的问题,这对我很有效:
li.noselection::selection {
background-color: transparent;
}
I tested it on Chrome, IE from 7 to 10 and Firefox.
我在Chrome、IE 7到10和Firefox上测试过。
P.S. This only disables the "visual effect", the text still gets selected. I hope that's why this got downvoted, because if your problem is only the aesthetics, this solution works 100%.
附注:这只是禁用“视觉效果”,文本仍然被选中。我希望这就是为什么它被否决的原因,因为如果你的问题只是美学,这个解决方案100%有效。
#6
3
To work with jQuery version above 1.9.x
要使用jQuery版本超过1.9.x
HTML
HTML
The html markup as shown above:
如上所示的html标记:
<ul>
<li>Go to the store</li>
<li>Pick up dinner</li>
<li>Debug crash</li>
<li>Take a jog</li>
</ul>
JS
JS
Use preventDefault() instead of return false which doesn't kill any other handlers you might have
使用preventDefault()而不是return false,这不会杀死您可能拥有的任何其他处理程序
$('li').on('selectstart', function (event) {
event.preventDefault();
});
Example: jsFiddle
例如:jsFiddle
#7
0
window.getSelection().empty()
works fine in Chrome, albeit with a quick flash of the selection, which is ugly.
在Chrome中工作得很好,尽管快速闪过选择,这是丑陋的。
#8
0
$( 'selector' ).on( 'selectstart', 'selector', function( ) {
return false;
}).css( 'MozUserSelect','none' ).mousedown( function( ) {
return false;
});
replace the selector with your own - this code works fine on all browsers. Using .on() for elements inserted dynamically to the DOM
用自己的代码替换选择器——这段代码在所有浏览器上都很好用。对动态插入到DOM的元素使用.on()
#9
0
document.onselectstart = function() { return false; }
document.onmousedown = function() { return false; }
#1
52
I'm writing on iPhone, while away from the desk, but a quick Google turned up this page: disable text selection with jQuery.
我是在iPhone上写的,虽然离开了桌面,但是一个快速的谷歌出现了这个页面:用jQuery禁用文本选择。
Edited in response to the 'dead link' comment (from @Herb Caudill). While the original link is, indeed, dead, it appears to be due to a site restructuring (rather than removal) and the new location for the article can be found here: http://chris-barr.com/index.php/entry/disable_text_selection_with_jquery/
编辑回应“死链接”评论(来自@Herb Caudill)。虽然最初的链接确实已死,但它似乎是由于站点重组(而不是删除),本文的新位置可以在这里找到:http://chris-barr.com/index.php/entry/disable_text_selection_with_jquery/
And the code provided in that article is reproduced below:
该条所载的守则如下:
$(function(){
$.extend($.fn.disableTextSelect = function() {
return this.each(function(){
if($.browser.mozilla){//Firefox
$(this).css('MozUserSelect','none');
}else if($.browser.msie){//IE
$(this).bind('selectstart',function(){return false;});
}else{//Opera, etc.
$(this).mousedown(function(){return false;});
}
});
});
$('.noSelect').disableTextSelect();//No text selection on elements with a class of 'noSelect'
});
jQuery snippet written by Chris Barr, of chris-barr.com, as accessed on Friday, 21st of January, 2011.
#2
35
If you use jQuery UI you can disable text selection as simple as that:
如果您使用jQuery UI,您可以禁用如下简单的文本选择:
$("body").disableSelection();
#3
33
I solved this using the non-standard CSS keyword user-select:
我用非标准的CSS关键字用户选择解决了这个问题:
.unselectable {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
#4
10
//function to be reused later
function clearSelection() {
var sel ;
if(document.selection && document.selection.empty){
document.selection.empty() ;
} else if(window.getSelection) {
sel=window.getSelection();
if(sel && sel.removeAllRanges)
sel.removeAllRanges() ;
}
}
$('p').click(clearSelection);
源
#5
6
I had the same problem and this worked for me:
我遇到了同样的问题,这对我很有效:
li.noselection::selection {
background-color: transparent;
}
I tested it on Chrome, IE from 7 to 10 and Firefox.
我在Chrome、IE 7到10和Firefox上测试过。
P.S. This only disables the "visual effect", the text still gets selected. I hope that's why this got downvoted, because if your problem is only the aesthetics, this solution works 100%.
附注:这只是禁用“视觉效果”,文本仍然被选中。我希望这就是为什么它被否决的原因,因为如果你的问题只是美学,这个解决方案100%有效。
#6
3
To work with jQuery version above 1.9.x
要使用jQuery版本超过1.9.x
HTML
HTML
The html markup as shown above:
如上所示的html标记:
<ul>
<li>Go to the store</li>
<li>Pick up dinner</li>
<li>Debug crash</li>
<li>Take a jog</li>
</ul>
JS
JS
Use preventDefault() instead of return false which doesn't kill any other handlers you might have
使用preventDefault()而不是return false,这不会杀死您可能拥有的任何其他处理程序
$('li').on('selectstart', function (event) {
event.preventDefault();
});
Example: jsFiddle
例如:jsFiddle
#7
0
window.getSelection().empty()
works fine in Chrome, albeit with a quick flash of the selection, which is ugly.
在Chrome中工作得很好,尽管快速闪过选择,这是丑陋的。
#8
0
$( 'selector' ).on( 'selectstart', 'selector', function( ) {
return false;
}).css( 'MozUserSelect','none' ).mousedown( function( ) {
return false;
});
replace the selector with your own - this code works fine on all browsers. Using .on() for elements inserted dynamically to the DOM
用自己的代码替换选择器——这段代码在所有浏览器上都很好用。对动态插入到DOM的元素使用.on()
#9
0
document.onselectstart = function() { return false; }
document.onmousedown = function() { return false; }