属性

2010-11-19 183 views
4

的索引值我使用下面的代码 ... ...属性

for($i=0; $i<90; $i++){ 
?> 
<a id='read[<?php print $i; ?>]' href="<?php print $textToshow; ?>"> Text Shown</a> 
<?php } ?> 

我想知道在href的ID时,就可以了用户点击。像读取的东西[1]读[2]等

回答

5
$('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>元素使用the class selector"someClass"

3

在这里你去

$('a[id^=read]').click(function(){ 
    alert(this.id); 

    return false; 
}); 

我用attribute-starts-with选择的目标是有一个idread

开始链接