简体   繁体   English

如何使div无法选择?

[英]How can I make a div unselectable?

I would like to make specific div's unselectable Double-clicks, etc. should be blocked. 我想使特定div的无法选择的Double-clicks等被阻止。

Here's what I've tried: 这是我尝试过的:

<div id="test" class="unselectable">asd</div>

$(document).ready(function() {
    $.ctrl = function(key, callback, args) {
        var isCtrl = false;
        $(document).keydown(function(e) {
            if (!args) args = []; // IE barks when args is null
            if (e.ctrlKey) isCtrl = true;
            if (e.keyCode == key.charCodeAt(65) && isCtrl) {
                callback.apply(this, args);
                return false;
            }
        }).keyup(function(e) {
            if (e.ctrlKey) isCtrl = false;
        });
    };

    //Other Method
    $(function() {
        $(document).keydown(function(objEvent) {
            if (objEvent.ctrlKey) {
                if (objEvent.keyCode == 65) {
                    objEvent.disableTextSelect();
                    return false;
                }
            }
        });
    });  
});​

But I find, to my chagrin, that this fails to work. 但令我恼火的是,我发现这没有用。 How might I modify my code to achieve my objective? 如何修改代码以实现目标?

You don't need Javascript for this ( reference answer ). 您不需要为此使用Javascript( 参考答案 )。 Do this: 做这个:

div {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

As far as I know, there is no way to stop the user from selecting the text on a div. 据我所知,没有办法阻止用户选择div上的文本。 Since this is a browser and potentially operating-system related feature, its beyond the realm of things you can typically manipulate with Javascript. 由于这是浏览器和与操作系统相关的功能,因此它超出了您通常可以使用Javascript处理的范围。

If you wanted to be incredibly annoying to your user, you could create an event handler on the entire document that would check if the user ever pressed control-A together, and if so, try to prevent the div from being selected by preventing the default action or focusing on some other part of the page. 如果您想让用户感到厌烦,则可以在整个文档上创建一个事件处理程序,以检查用户是否曾经同时按下Control-A;如果是,请尝试通过阻止默认值来防止选择div动作或专注于页面的其他部分。 However, this would do little more than piss them off that you are trying to keep them from using their computer the way they want to and probably make them hightail it out of your website. 但是,这样做只会激怒他们,因为您试图阻止他们以他们想要的方式使用他们的计算机,并可能使他们高调地退出您的网站。

Could you give us a little more background on why you don't want the user selecting something? 您能否为我们提供更多有关为什么您不希望用户选择某些内容的背景知识? There will always be a workaround to whatever you do, considering they can just disable javascript/CSS and reload the page. 考虑到他们可以禁用javascript / CSS并重新加载页面,因此无论您做什么,总会有一种解决方法。 If you want to make text a little more difficult to copy-paste you can make an image containing the desired text and place that in the div instead, but you're still just spending your time just to make the user more annoyed with your website. 如果您想使文本更难以复制粘贴,则可以制作一个包含所需文本的图像,并将其放置在div中,但是您仍然只是在浪费时间让用户对您的网站更加恼火。


While I'm not going to claim in the slightest that I know your entire project so well that I can tell you exactly what you should be doing, I will say there's probably a better way to accomplish whatever goal your working towards. 尽管我不会丝毫声称我对您的整个项目非常了解,以至于我可以确切地告诉您您应该做什么,但我会说,可能有更好的方法来实现您的工作目标。 If you post a little more detail, we can probably help you find it. 如果您发布更多细节,我们可能会帮助您找到它。 :D :D

I've answered at least three similar questions on SO with a fairly definitive answer. 我已经用一个确定的答案回答了至少三个类似的问题。 Here's the most popular . 这是最受欢迎的

I've also updated your jsFiddle with information from that answer to work in IE: http://jsfiddle.net/VYKfL/3/ 我还使用该答案中的信息更新了您的jsFiddle以在IE中工作: http : //jsfiddle.net/VYKfL/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM