属性的索引值
我使用以下代码……
for($i=0; $i <a id='read[]' href=""> Text Shown
我想知道用户点击它时href的id。 像读[1]读[2]等
$('a').click(function( e ) { alert(this.id); // e.preventDefault(); // Uncomment this line if you don't want }); // to follow the link's href.
这会将click
事件分配给所有 元素, 这些元素会在单击时提醒其ID。
取消注释e.preventDefault()
行以防止链接的默认行为(在其href
)。
最好在链接中添加一个类属性,然后选择使用:
$('a.someClass').click(function( e ) { alert(this.id); // e.preventDefault(); // Uncomment this line if you don't want });
干得好
$('a[id^=read]').click(function(){ alert(this.id); return false; });
我使用attribute-starts-with选择器来定位id
以read
开头的链接