javascript - Issues about disabling text selection and copying
为情所困
为情所困 2017-06-26 10:58:49
0
2
778
The style is as follows
.test1 {
  user-select: none;
}
html structure is as follows
<body>
<p class="content">
  <p class="test2">111</p>
  <p class="test1">222</p>
  <p class="test2">333</p>
  <p class="test1">444</p>
  <p class="test2">555</p>
</p>
</body>

We have disabled this article selection for some of the items. When selecting an item individually, the selection can indeed be disabled, and the content cannot be copied. However, if you use cmd a to select all, You will see that the disabled items are not selected on the page, but the content can be copied at this time.

Next use js to control:
[].forEach.call(document.querySelectorAll('.test1'), (node) => {
  node.addEventListener('copy', (e) => {
    e.stopPropagation();
    e.preventDefault();
    console.log(1);
    return false;
  }, true);
  node.addEventListener('selectstart', (e) => {
    e.stopPropagation();
    e.preventDefault();
    console.log(2);
    return false;
  }, true);
});

I found that it still takes effect when selecting individually, but it fails when selecting all. So I would like to ask if there is any way to intersperse unselectable nodes among the selectable nodes, and also make it possible to select only the selected nodes when copying. Optional content.

为情所困
为情所困

reply all(2)
为情所困

You can try another way, such as putting the text in css. Text in css cannot be selected and copied at all. I wrote a test code casually:

JsFiddle: https://jsfiddle.net/d95cugaL/

JsBin: http://jsbin.com/nowoxuceta/e...

Or go one step further and put the text directly on the image...

我想大声告诉你

Here is an idea, that is, you can disable the ctrl+c mouse event.
//Disable ctrl copy

document.onkeydown=function(){
    if((event.ctrlKey) && (window.event.keycode==67)){
          event.returnValue=false;
          alert("Ctrl+C被禁止啦!");
    }
}

If there is a problem with selecting all, you can also disable ctrl+A

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template