如何允许在禁止选择的帖子上选择一些文本
How to allow selecting some texts on the posts that are disabled from selecting?
我已经在StackExchange网站上问过这个问题。我在我的博客网站上添加了一个html/javascript代码,用于禁用内容复制。代码为:
<script src='demo-to-prevent-copy-paste-on-blogger_files/googleapis.js'></script><script type='text/javascript'> if(typeof document.onselectstart!="undefined" ) {document.onselectstart=new Function ("return false" ); } else{document.onmousedown=new Function ("return false" );document.onmouseup=new Function ("return false"); } </script>
如果我想允许只从页面中选择一些文本?例如,如果我想向读者提供一些代码,我如何允许他们只复制代码而不复制整个帖子?
提前感谢:)我的网站→www.aryanpoudel.com.np
首先,不能禁止从网站复制文本。所有文本都被发送到用户的浏览器,因此也可以被复制。这项措施只是不允许没有太多IT经验的用户不复制文本。
为了解决您的问题,最好使用CSS。有一个名为user-select
的CSS属性可以设置为none。这不允许用户选择元素上的文本,属性设置为。请确保同时使用浏览器前缀进行用户选择,因为并非所有没有前缀的主要浏览器都支持该选项。
.element {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
您可以在此处找到浏览器兼容性:http://caniuse.com/#feat=user-不选择
如果只想让页面的一部分可选择,可以通过将用户选择属性添加到页面的body
元素或*
选择器来实现。如果您想让某些元素可选,只需重置CSS样式,将属性再次更改回user-select: text
即可。
实现您想要的目标的另一种方法是:
function getSelectedText() {
if (window.getSelection) {
txt = window.getSelection();
} else if (window.document.getSelection) {
txt =window.document.getSelection();
} else if (window.document.selection) {
txt = window.document.selection.createRange().text;
}
return txt;
}
function removeSelection() {
if (window.getSelection) {
if (window.getSelection().empty) {
window.getSelection().empty();
} else if (window.getSelection().removeAllRanges) {
window.getSelection().removeAllRanges();
}
} else if (document.selection) {
document.selection.empty();
}
}
var wordSelectables = ["number", "video", "computer"];
document.onmouseup = function() {
var selText = getSelectedText().toString();
if (wordSelectables.indexOf(selText) == -1) {
removeSelection();
}
}
number video <p>video</p>computer
<input value="videocomputer">
相关文章:
- 用于选择/文本框操作的JavaScript
- 以编程方式选择文本Mobile Safari
- Firefox输入可以't在选择文本时滚动
- 如何在javascript中取消选择文本框
- 如何在html中打印选择文本
- SVG元素——处理和选择文本
- 选择文本并添加到本地存储
- 选择“p文本jquery”
- 为什么火狐在我更改 innerHTML 时会选择文本
- 根据换行符选择文本
- 如何按从当前单词到段落中该句子(.)结尾的范围选择文本
- 单击按钮时选择文本
- JavaScript 触发器事件在 Android 上选择文本
- .val() 选择文本而不是值字符串,当网页动态编辑源 html
- 为什么不用特殊字符替换选择文本,以及如何删除此特殊字符
- 使用链接多次选择文本
- 使用jQuery在焦点上选择文本框是't在移动浏览器中工作
- Safari存在文本输入问题,用户输入时会选择文本,导致文本丢失
- Bootstrap下拉菜单-仅复选框不选择文本
- 使用.on的多个下拉菜单选择文本仅适用于第一个下拉菜单