javascript:禁用文本选择

javascript:禁用文本选择,javascript,Javascript,我正在使用javascript禁用webiste上的文本选择 代码是: <script type="text/JavaScript"> function disableselect(e) { return false } function reEnable() { return true } document.onselectstart = new Function ("return false") if (window.sidebar) { document.o

我正在使用javascript禁用webiste上的文本选择

代码是:

<script type="text/JavaScript">

function disableselect(e) {
  return false
}

function reEnable() {
  return true
}

document.onselectstart = new Function ("return false")

if (window.sidebar) {
  document.onmousedown = disableselect
  document.onclick = reEnable
}
</script>

功能禁用选择(e){
返回错误
}
函数可重入(){
返回真值
}
document.onselectstart=新函数(“返回false”)
if(窗口侧栏){
document.onmousedown=禁用选择
document.onclick=可重入
}
类似的脚本也可以找到

在我的本地主机上:所有浏览器(Firefox、Chrome、IE和Safari)都运行良好

在我的直播网站上:除了Firefox之外,一切正常

我的问题是:

  • 是否有人对Firefox在直播站点和本地主机上的行为有所不同提出了建议。注意:Javascript已启用

  • 也许我的脚本太简单了,所以我尝试了完全相同的结果


  • 只需使用以下css方法:

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

    您可以在这里找到相同的答案:

    对于JavaScript,请使用此函数:

    function disableselect(e) {return false}
    document.onselectstart = new Function (return false)
    document.onmousedown = disableselect
    
    要启用选择,请使用以下命令:

    function reEnable() {return true}
    
    并在任何需要的地方使用此功能:

    document.onclick = reEnable
    

    我正在编写滑块ui控件以提供拖动功能,这是我防止用户拖动时选择内容的方法:

    function disableSelect(event) {
        event.preventDefault();
    }
    
    function startDrag(event) {
        window.addEventListener('mouseup', onDragEnd);
        window.addEventListener('selectstart', disableSelect);
        // ... my other code
    }
    
    function onDragEnd() {
        window.removeEventListener('mouseup', onDragEnd);
        window.removeEventListener('selectstart', disableSelect);
        // ... my other code
    }
    
    在dom上绑定
    startDrag

    <button onmousedown="startDrag">...</button>
    

    如果您有这样一个html页面:

    <body onbeforecopy = "return false" ondragstart = "return false" onselectstart = "return false" oncontextmenu = "return false" onselect = "document.selection.empty()" oncopy = "document.selection.empty()"> 有一种简单的方法可以禁用所有事件:

    document.write(document.body.innerHTML)


    您获得了html内容,但丢失了其他内容。

    简单地复制此文本,然后在前面添加


    也可以使用,在所有浏览器中都可以正常工作,需要javascript:

    onselectstart = (e) => {e.preventDefault()}
    
    例如:
    onselectstart=(e)=>{
    e、 预防默认值()
    console.log(“nope!”)
    }

    选择我我建议您避免在这种情况下使用Javascript。只需使用CSS,您就可以在CSS中找到有关文本选择的更多信息—第二个脚本可能太旧了。拆下!因为我相信它打破了fx的支持,所以我讨厌网站这样做。如果我想复制一些文本,我可以打开开发工具并复制它。这只是一个不必要的不便,正如瑞克所说。除非你有很好的理由,否则不要这样做。许多人可能只是想选择一些东西来搜索它,或者使用字典来查找一个单词。禁止这只是一个烦人的问题,从长远来看,这可能会让人们远离你的网站。首先,我也讨厌仅仅为了“乐趣”而禁止选择(文本和其他内容)的页面,但我认为这是一个好问题,因为想要禁用它有一些正当的理由(例如,在任何游戏中实现鼠标手势或拖放文本块)。当然,使用JS来实现这一目的是不好的,但CSS解决方案也有其问题(在某些浏览器中,
    user select
    单独无法工作,它需要“-vendor-”前缀,如
    -moz user select
    -webkit user select
    ,因此,要使其正常工作,必须将所有可能的变体设置为
    none
    )。同时添加你找到答案的地方的链接:非常感谢。除了IE,CSS在身体中适用于所有情况。是否有简洁的CSS解决方案也可以解决IE的这个问题?我现在使用Js和CSS的组合来覆盖所有浏览器!!thk u.1。为什么这个asnwer没有被标记为解决方案?…它解决了问题以最准确、最简单的方式。2.应该提到的是,这种CSS定义可以在任何HTML元素上实现,而不仅仅是正文。@TheCuBeMan“user select”是一种非标准功能,因此IMO不是最佳解决方案。这可能是最佳方法-浏览器处理它的方式不同,因为它很复杂,而且它们都有不同的处理方式。也就是说,它们希望提供预期的UX(无选择)同时仍然允许用户选择他们是否真的想要。W3C有更多的注释,并且显示了极好的支持。浏览器以不同的方式处理它是可以的-用户将获得他们首选的浏览器提供的体验。这是最好的解决方案,因为CSS解决方案不适用于具有
    contenteditable 属性(如在用户鼠标调整控件大小时要禁用文本选择)。除了
    new Function(return false)
    不是有效的javascript代码。仅使用
    document.onselectstart=disableselect
    @amik如何:
    document.oncontextmenu=new Function(“return false;”);
    或使用CSS的方法:
    新函数(“return false;”)
    可以工作,但与仅使用您已有的
    禁用选择功能相比,它不必要地复杂。
    onselectstart=“return false”
    也可以,但与CSS无关。另外,我注意到你的其余答案也是错误的,
    可重入
    函数根本不起作用。@amik haha好吧,JavaScript中没有什么必要的东西,但我们仍然可以。当然,代码可以重构,但看看问题,回答,回答不是想做家庭作业。但是,这是一个教育的地方,所以我认为提供一个技术上是错误的,而不是解释,不是一个好主意。
    function disableselect(e) {
      return false
    }
    
    function reEnable() {
      return true
    }
    
    document.onselectstart = new Function ("return false")
    
    if (window.sidebar) {
      document.onmousedown = disableselect
      document.onclick = reEnable
    }
    
    onselectstart = (e) => {e.preventDefault()}