我正在使用以下代码
...
...
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
事件至all <a>
单击时将提醒其 ID 的元素。
取消注释e.preventDefault() http://api.jquery.com/event.preventdefault/行以防止链接的默认行为(遵循其href
).
最好向链接添加一个类属性,然后选择使用该属性:
$('a.someClass').click(function( e ) {
alert(this.id);
// e.preventDefault(); // Uncomment this line if you don't want
});
这选择<a>
元素与类"someClass"
using 类选择器 http://api.jquery.com/class-selector/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)