我正在使用以下代码 ... ...
for($i=0; $i<90; $i++){
?>
<a id='read[<?php print $i; ?>]' href="<?php print $textToshow; ?>"> Text Shown</a>
<?php } ?>
我想知道用户点击它时href的ID。像read [1] read [2] etc
之类的东西答案 0 :(得分:5)
$('a').click(function( e ) {
alert(this.id);
// e.preventDefault(); // Uncomment this line if you don't want
}); // to follow the link's href.
这会将<{1}}事件分配给所有 click
元素,这些元素会在点击时提醒其ID。
取消注释e.preventDefault()
行以防止链接的默认行为(在<a>
之后)。
最好将类属性添加到链接中,然后使用:
进行选择href
使用the class selector选择$('a.someClass').click(function( e ) {
alert(this.id);
// e.preventDefault(); // Uncomment this line if you don't want
});
个<a>
个元素。
答案 1 :(得分:3)
你去吧
$('a[id^=read]').click(function(){
alert(this.id);
return false;
});
我使用attribute-starts-with选择器来定位以{strong> id
read
的链接