jQuery或jQuery-UI是否具有禁用指定文档元素的文本选择功能?
jQuery或jQuery-UI是否具有禁用指定文档元素的文本选择功能?
(function($){
$.fn.disableSelection = function() {
return this
.attr('unselectable', 'on')
.css('user-select', 'none')
.on('selectstart', false);
};
})(jQuery);
如果你使用jQuery UI,那么有一个专门的方法来处理这个问题,但它只能处理鼠标选择(即CTRL+A仍然有效):
$('.your-element').disableSelection(); // deprecated in jQuery UI 1.9
如果你不想使用jQuery UI,那么这段代码非常简单:
$(el).attr('unselectable','on')
.css({'-moz-user-select':'-moz-none',
'-moz-user-select':'none',
'-o-user-select':'none',
'-khtml-user-select':'none', /* you could also put this in a class */
'-webkit-user-select':'none',/* and add the CSS class here instead */
'-ms-user-select':'none',
'user-select':'none'
}).bind('selectstart', function(){ return false; });
.css()
方法中,我们是否仍需要包含供应商前缀? - Ari我发现这个答案(Prevent Highlight of Text Table)非常有帮助,也许可以与提供IE兼容性的另一种方法结合使用。
#yourTable
{
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
user-select: none;
}
这里是更全面的解决方案,能够处理断开选择和取消一些快捷键的操作(例如Ctrl+a和Ctrl+c)。测试: Cmd+a和Cmd+c)
(function($){
$.fn.ctrlCmd = function(key) {
var allowDefault = true;
if (!$.isArray(key)) {
key = [key];
}
return this.keydown(function(e) {
for (var i = 0, l = key.length; i < l; i++) {
if(e.keyCode === key[i].toUpperCase().charCodeAt(0) && e.metaKey) {
allowDefault = false;
}
};
return allowDefault;
});
};
$.fn.disableSelection = function() {
this.ctrlCmd(['a', 'c']);
return this.attr('unselectable', 'on')
.css({'-moz-user-select':'-moz-none',
'-moz-user-select':'none',
'-o-user-select':'none',
'-khtml-user-select':'none',
'-webkit-user-select':'none',
'-ms-user-select':'none',
'user-select':'none'})
.bind('selectstart', false);
};
})(jQuery);
并调用示例:
$(':not(input,select,textarea)').disableSelection();
这可能对于旧版本的FireFox来说还不够(我无法确定是哪个版本)。如果以上方法都不起作用,请添加以下内容:
.on('mousedown', false)
attr('unselectable', 'on')
?这是一个打字错误还是有用的操作? - KajMagnus$(".item")
.attr('unselectable', 'on')
.css({
'user-select': 'none',
'MozUserSelect': 'none'
})
.on('selectstart', false)
.on('mousedown', false);
这其实非常简单。 要禁用文本选择(以及点击+拖动文本(例如在Chrome中的链接),只需使用以下jQuery代码:
$('body, html').mousedown(function(event) {
event.preventDefault();
});
这只是在你用鼠标点击(mousedown()
)body
和html
标签时,防止默认事件的发生。你可以通过改变两个引号之间的文本(例如将$('body, html')
更改为$('#myUnselectableDiv')
,使myUnselectableDiv
div不能被选择。
以下是一个快速示例以向您展示/证明这一点:
$('#no-select').mousedown(function(event) {
event.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="no-select">I bet you can't select this text, or drag <a href="#">this link</a>, </span>
<br/><span>but that you can select this text, and drag <a href="#">this link</a>!</span>
$(document).ready(function(){
$("body").css("-webkit-user-select","none");
$("body").css("-moz-user-select","none");
$("body").css("-ms-user-select","none");
$("body").css("-o-user-select","none");
$("body").css("user-select","none");
});
这可以通过 JavaScript 轻松完成,适用于所有浏览器。
<script type="text/javascript">
/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
function disableSelection(target){
if (typeof target.onselectstart!="undefined") //For IE
target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //For Firefox
target.style.MozUserSelect="none"
else //All other route (For Opera)
target.onmousedown=function(){return false}
target.style.cursor = "default"
}
</script>
<script type="text/javascript">
disableSelection(document.body)
</script>
document.onselectstart = new Function('return false;');
对我来说完美地工作!
添加 CSS:
.no_select { user-select: none; -o-user-select: none; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -ms-user-select:none;}
添加jQuery:
(function($){
$.fn.disableSelection = function()
{
$(this).addClass('no_select');
if($.browser.msie)
{
$(this).attr('unselectable', 'on').on('selectstart', false);
}
return this;
};
})(jQuery);
可选:如果您想禁用所有子元素的选择,您可以将IE块更改为:
$(this).each(function() {
$(this).attr('unselectable','on')
.bind('selectstart',function(){ return false; });
});
用法:
$('.someclasshere').disableSelection();