1

nth-childとifelseステートメントを使用して次のことを実現することは可能ですか?

9つのdivがあり、それぞれにリンクがあり、クリックすると機能が実行されるとします。どういうわけか、リンクに(bodyタグから)n番目の子のdivが含まれていることを認識させ、3番目、6番目、または9番目のdiv内にある場合にのみ関数を実行できますか?どこから始めればいいのか本当にわかりませんが、可能だと思います。

4

3 に答える 3

4

You can use nth-clild,

Live Demo

$('.divclass :nth-child(3n)')

For binding event, you can use click()

Live demo

$('#divId div:nth-child(3n) a').click(function(){
  alert($(this).text());    
});​
于 2012-12-18T05:52:01.960 に答える
2

jsBinデモ

$('div:nth-child(3n)').find('a').click(function( e ){

  e.preventDefault();

  alert( $(this).text() );

});
于 2012-12-18T05:55:34.687 に答える
0
$('divclass:nth-child(3n)').find('a').on('click',function(){
    alert( $(this).text() );
});

これがうまくいくことを願っています:)乾杯

于 2012-12-18T06:05:39.440 に答える