Tow*_*wer 79 html javascript selection
我在HTML页面上有一个div,每当我按下鼠标并移动它时,它会显示"不能掉落"光标,就像选择一些东西一样.有没有办法禁用选择?我试过CSS用户选择没有成功.
Tim*_*own 159
专有变体user-select
将适用于大多数现代浏览器:
*.unselectable {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
/*
Introduced in IE 10.
See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
*/
-ms-user-select: none;
user-select: none;
}
Run Code Online (Sandbox Code Playgroud)
对于IE <10和Opera,您需要使用unselectable
您希望不可选择的元素的属性.您可以使用HTML中的属性进行设置:
<div id="foo" unselectable="on" class="unselectable">...</div>
Run Code Online (Sandbox Code Playgroud)
遗憾的是,这个属性不是继承的,这意味着你必须在一个内部的每个元素的开始标记中放置一个属性<div>
.如果这是一个问题,您可以使用JavaScript以递归方式为元素的后代执行此操作:
function makeUnselectable(node) {
if (node.nodeType == 1) {
node.setAttribute("unselectable", "on");
}
var child = node.firstChild;
while (child) {
makeUnselectable(child);
child = child.nextSibling;
}
}
makeUnselectable(document.getElementById("foo"));
Run Code Online (Sandbox Code Playgroud)
mol*_*oco 10
似乎CSS用户选择不会阻止图像拖放...所以..
HTML:
<img src="ico.png" width="20" height="20" alt="" unselectable="on" /> Blabla bla blabla
Run Code Online (Sandbox Code Playgroud)
CSS:
* {
user-select: none;
-khtml-user-select: none;
-o-user-select: none;
-moz-user-select: -moz-none;
-webkit-user-select: none;
}
::selection { background: transparent;color:inherit; }
::-moz-selection { background: transparent;color:inherit; }
Run Code Online (Sandbox Code Playgroud)
JS:
$(function(){
$('*:[unselectable=on]').mousedown(function(event) {
event.preventDefault();
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
我使用cancelBubble=true
并stopPropagation()
在鼠标中向下移动处理程序.
归档时间: |
|
查看次数: |
63980 次 |
最近记录: |