jQuery获取某元素下所有的链接元素:
在某些情况下需要获得某些元素下的所有链接,下面就介绍一下如何实现此效果。
代码实例如下:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://www.softwhy.com/" /> <title>jQuery获取元素所有的链接</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script language="javascript" type="text/javascript"> $(function(){ $("#allA a").click(function(){ window.alert(this.href); return false; }) }) </script> </head> <body> <div id="allA"> <a href="http://www.softwhy.com/">蚂蚁部落</a> <a href="http://www.163.com/">网易</a> <a href="http://www.qq.com/">腾讯</a> <a href="http://www.sina.com/">新浪</a> </div> </body> </html>
以上代码中,当点击链接时,能够弹出相应链接的href属性值。
需要注意的几点:
1.$("#allA a")获取id为allA元素元素下的所有链接。
2.this.href中的this是被点击的当前链接对象。
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=7859
更多内容可以参阅:http://www.softwhy.com/jquery/