您的位置:首页 > 运维架构

Firefox/Opera不支持onselectstart事件

2012-09-06 18:06 507 查看
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Firefox/Opera不支持onselectstart事件</title>
  </head>  
  <body>
    <div id="d1" style="width:200px;height:200px;background:gold;">Text Text</div>
    <script type="text/javascript">
     
        var div = document.getElementById('d1');
        div.onselectstart = function(){
            console.log(3);
        }
    </script>
  </body>
</html>


当用鼠标去选定div内的文本时,IE/Safari/Chrome 的控制台输出了3,Firefox/Opera则没有输出。

1 IE可以使用onselectstart事件来阻止用户选定元素内文本,如下
<div onselectstart="return false">accc</div>


2 Firefox中可以使用CSS "-moz-user-select:none"属性来禁止文本选定

3 webkit浏览器可以使用“-khtml-user-select”,当然也可以使用方式1

CSS3 定义了“user-select”属性,如下



可惜所有浏览器都未实现,如FF4/Safar5/Chrome11/Opera10/IE10。

相关:

https://developer.mozilla.org/en/CSS/-moz-user-select

http://msdn.microsoft.com/en-us/library/ms536969%28VS.85%29.aspx

http://www.w3.org/TR/2000/WD-css3-userint-20000216#user-select
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: