是否有可能使用第n个子代和if else语句来实现以下结果?
假设有9个div,每个div都有一个链接,当单击该链接时,将执行一个函数。我能不能让链接找出它是第n个子div (来自body标签),并仅当它位于第3,6或9个div内时才执行该函数?我真的不知道从哪里开始,但这似乎是可能的。
发布于 2012-12-18 13:52:02
您可以使用,
$('.divclass :nth-child(3n)')对于绑定事件,可以使用click()
$('#divId div:nth-child(3n) a').click(function(){
  alert($(this).text());    
});发布于 2012-12-18 13:55:35
jsBin demo
$('div:nth-child(3n)').find('a').click(function( e ){
  e.preventDefault();
  alert( $(this).text() );
});发布于 2012-12-18 14:05:39
$('divclass:nth-child(3n)').find('a').on('click',function(){
    alert( $(this).text() );
});希望这行得通:)干杯
https://stackoverflow.com/questions/13927048
复制相似问题