---
title: "Firefox/Opera不支持onselectstart事件"
author: "zhouyrt"
date: "2011-08-21T00:11:25Z"
tags: []
categories: ["Web前端"]
slug: "firefox-opera-onselectstart"
draft: false
lastmod: "2011-08-21T00:11:25Z"
id: 1152669
url: "https://snandy.iteye.com/blog/1152669"
---

如下

 

```
<!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”属性，如下

![](http://dl.iteye.com/upload/attachment/539022/fcf41e10-882c-3a10-93c4-84da646bac24.png)

 

 

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

 

相关：

[https://developer.mozilla.org/en/CSS/-moz-user-select](https://developer.mozilla.org/en/CSS/-moz-user-select)

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

[http://www.w3.org/TR/2000/WD-css3-userint-20000216#user-select](http://www.w3.org/TR/2000/WD-css3-userint-20000216#user-select)