Firefox/Opera不支持onselectstart事件

如下

<!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 

posted on 2011-06-01 18:36  snandy  阅读(5234)  评论(1编辑  收藏  举报