js设置连接不可选中

JavaScript016

js设置连接不可选中,第1张

新建一个html文件,命名为test.html,用于讲解如何在javascript中让链接变成灰色,不可点击。

2.在test.html文件内,使用a标签创建一个链接,并设置其id为link,用于下面获得a标签对象。

3.在test.html文件内,使用button标签创建一个按钮,给button按钮绑定onclick点击事件,当按钮被点击时

方法有两种:

一. css方法

只须在html标签中添加 属性: onselectstart = "return false" 即可.

二.js方法

if (typeof(element.onselectstart) != "undefined") {

// IE下禁止元素被选取

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

} else {

// firefox下禁止元素被选取的变通办法

element.onmousedown = new Function("return false")

element.onmouseup = new Function("return true")

}