属性的索引值

时间:2010-11-19 01:11:31

标签: jquery fancybox

我正在使用以下代码 ...  ...

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

之类的东西

2 个答案:

答案 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的链接