在jquery中,可以利用removeAttr()方法讓超鏈接禁用,該方法能夠刪除元素中的屬性,只要刪除超鏈接元素的“href”屬性即可,語法為“超鏈接元素對象..removeAttr('href');”。
本教程操作環境:windows10系統、jquery3.2.1版本、Dell G3電腦。
jquery怎么讓超鏈接不可用
removeAttr() 方法從被選元素移除一個或多個屬性。
語法
$(selector).removeAttr(attribute)
attribute 必需。規定要移除的一個或多個屬性。如需移除若干個屬性,請使用空格分隔屬性名稱。
去掉a標簽中的href屬性
示例如下:
<html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("a").removeAttr('href'); }); }); </script> </head> <body> <a href="http://www.php.cn">這是一個鏈接</a> <button>禁用這個鏈接</button> </body> </html>
輸出結果:
相關視頻教程推薦:jQuery視頻教程