如何在html中禁用特定元素的复制

时间:2017-05-26 11:53:35

标签: javascript jquery html css

在这里,当选择3个元素时,我的复制选项出现问题。但是,我有一个Jquery函数来禁用中间元素的复制。如何在选择3个元素时禁用它。但是,如果我单独选择中间元素,则不会复制。



$('#notcp').bind('cut copy paste', function (e) {
    e.preventDefault();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p >
select and copy
</p>
<p id="notcp">
cannot copy
</p>
<p>
select and copy
</p>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

有很多方法可以实现这一目标。

最简单的方法是使用jQuery的.hide()方法

$("#notcp").hide();

要再次显示它,请使用show()方法

$("#notcp").show();

答案 1 :(得分:0)

尝试添加return false;

&#13;
&#13;
   $('#notcp').bind('copy paste',function(e) {
    e.preventDefault(); return false; 
});
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用此CSS样式禁用选择。通过这个文本将不会被选中。因此也无法复制。

#notcp {
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}

答案 3 :(得分:0)

试试这个。

$(document).on("cut copy paste","#notcp",function(e) {
    e.preventDefault();
});