2009-05-24 56 views
2

下面的代碼僅適用於第一格。當我點擊其他「img」時,我無法顯示其他div。 你知道一個方法來達到第二個(下一個)元素嗎?如何在點擊圖像時用JQUERY選擇第二個元素?

$(document).ready(function() { 

     $('.imgOpenClose').click(function() { 
      if ($(this).attr("src") == "images/openTree.gif") { 
       $(this).attr("src", "images/closeTree.gif"); 
       $(this).parent().find('div').eq(0).show(); 
      } 
      else { 
       $(this).attr("src", "images/openTree.gif"); 
       $(this).parent().find('div').eq(0).hide(); 
      } 
     }); 
}); 

Wanna open table when i clicked on plus images

And wanna see the table inside this div

+0

你能不能給我們你想要的網頁一些示例HTML你的JavaScript運行?否則,你的問題缺乏細節,我們無法真正幫助你。 – rampion 2009-05-24 12:12:15

回答

2

既然你有像下面休息元素,你需要有一個過濾器使用nextAll並選擇第一個DIV,而不是簡單地使用next的如我原始答案。

$('.imgOpenClose').click(function() { 
    $(this).nextAll('div:first').toggle(); 
}); 
+0

不應該是$(this).next('div')嗎? – markusk 2009-05-24 12:16:30

+0

@markusk - 好,我認爲DIV都在同一水平。 – tvanfosson 2009-05-24 12:22:16

1

如果你有幾個圖像與同一類標籤,你仍然可以做到這一點,而不必附加一個唯一的ID:

$(function() { 
    $('.imgOpenClose').click(function() { 
     $(this).next('div').children('table').toggle(); 
    }); 
}); 
相關問題