Ben*_*Ben 171 html css cross-browser highlighting textselection
我想在我的网页上添加文字作为标签并使其无法选择.
换句话说,当鼠标光标在文本上方时,我希望它根本不会变成文本选择光标.
我想要实现的一个很好的例子就是这个网站上的按钮(问题,标签,用户......)
Bal*_*usC 242
你不能用普通的HTML来做到这一点,所以JSF也不能为你做很多事情.
如果你只针对不错的浏览器,那么只需使用CSS3:
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Run Code Online (Sandbox Code Playgroud)
<label class="unselectable">Unselectable label</label>
Run Code Online (Sandbox Code Playgroud)
如果您还想覆盖旧浏览器,请考虑此JavaScript后备:
<!doctype html>
<html lang="en">
<head>
<title>SO question 2310734</title>
<script>
window.onload = function() {
var labels = document.getElementsByTagName('label');
for (var i = 0; i < labels.length; i++) {
disableSelection(labels[i]);
}
};
function disableSelection(element) {
if (typeof element.onselectstart != 'undefined') {
element.onselectstart = function() { return false; };
} else if (typeof element.style.MozUserSelect != 'undefined') {
element.style.MozUserSelect = 'none';
} else {
element.onmousedown = function() { return false; };
}
}
</script>
</head>
<body>
<label>Try to select this</label>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果您已经在使用jQuery,那么这是另一个disableSelection()向jQuery 添加新函数的示例,以便您可以在jQuery代码中的任何位置使用它:
<!doctype html>
<html lang="en">
<head>
<title>SO question 2310734 with jQuery</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.fn.extend({
disableSelection: function() {
this.each(function() {
if (typeof this.onselectstart != 'undefined') {
this.onselectstart = function() { return false; };
} else if (typeof this.style.MozUserSelect != 'undefined') {
this.style.MozUserSelect = 'none';
} else {
this.onmousedown = function() { return false; };
}
});
}
});
$(document).ready(function() {
$('label').disableSelection();
});
</script>
</head>
<body>
<label>Try to select this</label>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Blo*_*sie 167
这里没有人发布所有正确CSS变体的答案,所以这里是:
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
Run Code Online (Sandbox Code Playgroud)
Vic*_*eld 52
解决问题的完全现代解决方案纯粹基于CSS,但请注意旧版浏览器不支持它,在这种情况下,您需要回退到其他人提供的解决方案.
所以在纯CSS中:
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
Run Code Online (Sandbox Code Playgroud)
但是,当超过元素的文本时,鼠标光标仍将更改为插入符号,因此您添加到:
cursor: default;
Run Code Online (Sandbox Code Playgroud)
现代CSS非常优雅.
小智 11
我修改了上面发布的jQuery插件,因此它可以在live元素上运行.
(function ($) {
$.fn.disableSelection = function () {
return this.each(function () {
if (typeof this.onselectstart != 'undefined') {
this.onselectstart = function() { return false; };
} else if (typeof this.style.MozUserSelect != 'undefined') {
this.style.MozUserSelect = 'none';
} else {
this.onmousedown = function() { return false; };
}
});
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
那么你可以这样:
$(document).ready(function() {
$('label').disableSelection();
// Or to make everything unselectable
$('*').disableSelection();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
210165 次 |
| 最近记录: |