2010-11-19 11 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. 

これは、クリックされたときにそのIDを警告するすべて<a>要素にclickイベントを割り当てます。

e.preventDefault()の行のコメントを解除して、リンクのデフォルト動作(hrefに従う)を防止します。

それはおそらくリンクにclass属性を追加し、それを使用して選択するのがベストでしょう:

$('a.someClass').click(function(e) { 
    alert(this.id); 
    // e.preventDefault(); // Uncomment this line if you don't want 
}); 

これはthe class selectorを使用して、クラス"someClass"<a>要素を選択します。

3

ここでは、私がread

で始まる idを持っているリンクをターゲットに attribute-starts-withセレクタを使用

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

    return false; 
}); 

を行きます

関連する問題