我正在使用以下代码 ... ...
for($i=0; $i<90; $i++){
?>
<a id='read[<?php print $i; ?>]' href="<?php print $textToshow; ?>"> Text Shown</a>
<?php } ?>
我想知道当用户点击 a href 时它的 id。类似 read[1] read[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
事件全部<a>
单击时将提醒其 ID 的元素。
取消注释 e.preventDefault()
行以防止链接的默认行为(遵循其 href
)。
最好向链接添加一个类属性,然后选择使用它:
$('a.someClass').click(function( e ) {
alert(this.id);
// e.preventDefault(); // Uncomment this line if you don't want
});
这选择<a>
具有类 "someClass"
的元素使用the class selector .
关于jquery - 属性的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4221269/