2013-06-11 61 views
5

我有這部分多次在一個頁面:如何選擇下一個div?

<a class="showComment" style="cursor: pointer;"><i class="icon-comment"></i> Views</a> 
<br /> 
<br /> 
<div class="writeComment" style="height: auto; width: 700px;" dir="ltr" hidden="hidden"> 
</div> 

我現在在一個js文件編寫代碼爲a.showComment點擊event.Now我想選擇下一個div.writeComment。如何選擇呢?

+1

define'select'it?喜歡,你想用它做什麼? – PlantTheIdea

+2

http://stackoverflow.com/questions/9629213/jquery-select-next-div-element?rq=1 –

回答

3

next()did not work。

nextAll()是所有的.writeComment元素都在我的.showComment元素之後。但是我發現了這個問題。下面的代碼做了它。

$('.showComment').click(function(){ 
    $(this).nextAll(".writeComment").first(); 
}); 
-1

我假設你使用jQuery。否則,我強烈建議你這樣做。 它有一個next-statement:紅色:

$('#foo').next().css('background-color', 'red'); 

FOO個背景後設置元素。

2

這可能使用你展示正確的方式下一個()http://api.jquery.com/next/ 的選擇將是如下:

這樣:

$('.showComment').next('.writeComment') 

或:

$('.showComment').next().find('.writeComment') 
+0

@PlantTheIdea謝謝你的改進:)非常感謝的歡呼聲! –

+0

不用擔心,我沒有做太多哈哈。拼寫錯誤是唯一真正的問題,我只是不喜歡在選擇器上看到標籤限定符,因爲它不必要,只會降低效率。 – PlantTheIdea

3

在變nextDiv你可以做任何你想做的事
使用.nextAll()方法可以讓我們搜索通過DOM樹中這些元素的繼承者,並從匹配元素構造一個新的jQuery對象。使用.next()

,而不是你,你已經點擊 試試這個DOM元素之後搜索:

$('.showComment').click(function(){ 
     var nextDiv = $(this).nextAll("div.writeComment"); 
    }); 

或者

$('.showComment').click(function(){ 
      var nextDiv = $('.showComment').next().find('.writeComment') 
     }); 

或者

$('.showComment').click(function(){ 
     var nextDiv = $(this).next("div.writeComment"); 
    }); 
-1

使用~選擇。 JQuery Reference

$('a.showComment ~ div.writeComment') 
+0

我愛downvotes沒有評論... :( – Andy