开发者

CSS / JS: How do I tell my page to be completely unselectable, except for stuff inside a div?

开发者 https://www.devze.com 2023-04-12 12:33 出处:网络
I\'m doing a live editing thing, but only want certain portions of the page to be selectable. How do I disable selection开发者_运维技巧 on everything except within a single div?Perhaps the CSS propert

I'm doing a live editing thing, but only want certain portions of the page to be selectable. How do I disable selection开发者_运维技巧 on everything except within a single div?


Perhaps the CSS property user-select ?

cross-browser :

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;

Doc :

http://www.w3.org/TR/2000/WD-css3-userint-20000216.html#user-select

Perhaps you could add the following listeners to the divs / sections you want to prevent selection ?

window.onload = function() {
  var element = document.getElementById('content');
  element.onselectstart = function () { return false; } // ie
  element.onmousedown = function () { return false; } // the rest
}

EDIT: See comments


Put a div (positioned absolute) on top of the elements you want to make un-selectable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号