Rei*_*gel 51 javascript jquery text
我有这个jQuery切换.它工作正常.
<ul>
<li>Go to the store</li>
<li>Pick up dinner</li>
<li>Debug crash</li>
<li>Take a jog</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
$("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":""});
}
);
Run Code Online (Sandbox Code Playgroud)
问题是当我快速点击时,它会突出显示其中的文字.有没有办法阻止文本突出显示?
Dav*_*mas 53
我在iPhone上写字,而远离桌面,但是快速谷歌出现了这个页面:用jQuery禁用文本选择.
编辑以回应"死链接"评论(来自@Herb Caudill).虽然原来的链接确实已经死了,但似乎是由于网站重组(而不是删除),文章的新位置可以在这里找到:http://chris-barr.com/index.php/进入/ disable_text_selection_with_jquery /
该条款中提供的代码转载如下:
$(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'
});
Run Code Online (Sandbox Code Playgroud)
Vis*_*ioN 36
如果您使用jQuery UI,则可以禁用文本选择,如下所示:
$("body").disableSelection();
Run Code Online (Sandbox Code Playgroud)
Sjo*_*erd 36
我使用非标准CSS关键字user-select解决了这个问题:
.unselectable {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*ini 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);
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,这对我有用:
li.noselection::selection {
background-color: transparent;
}
Run Code Online (Sandbox Code Playgroud)
我在Chrome,IE从7到10和Firefox上测试过它.
PS这只会禁用"视觉效果",文本仍会被选中.我希望这就是为什么这个被低估了,因为如果你的问题只是美学,那么这个解决方案可以100%运行.
使用 1.9.x 以上的 jQuery 版本
超文本标记语言
html标记如上所示:
<ul>
<li>Go to the store</li>
<li>Pick up dinner</li>
<li>Debug crash</li>
<li>Take a jog</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
JS
使用PreventDefault()而不是return false,这不会杀死您可能拥有的任何其他处理程序
$('li').on('selectstart', function (event) {
event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
示例:jsFiddle
归档时间: |
|
查看次数: |
42847 次 |
最近记录: |