2014-08-27 102 views
0

所有節點的文字我有這樣一個div:jQuery的如何選擇DIV

<div class="movie"> 

    Date: 
    <span class="date"> June 12, 1987 </span> 

    Título: 
    <span class="title"> 
     <a class="select_movie" href="#"> 
      Predator 
     </a>   
    </span> 

    Director: <span class="director">John McTiernan</span> 

    Starring: <span class="starring">Arnold Schwarzenegger</span> 

</div> 

我想在<div class="movie">所有文本節點,鏈接.select_movie

$('.select_movie').click(function(){ 
    .......... 
    .......... 
}); 

的在點擊後我需要檢索要分配給變量的節點的值的結尾,如下所示:

var date = June 12, 1987; 

var title = Predator; 

var director = John McTiernan; 

var starring = Arnold Schwarzenegger; 

回答

2

T RY這個爲了得到每個值:

$('.select_movie').click(function(){ 
    var movie = $(this).closest(".movie"); 
    var date = $(movie).find(".date").text(); 
    var title = $(movie).find(".title .select_movie").text(); 
    var director = $(movie).find(".director").text(); 
    var starring = $(movie).find(".starring").text(); 
}); 

小提琴http://jsfiddle.net/unwgeqce/1/

+0

猜測這裏,但可能有多個電影上這一頁。也許追求最接近的.movi​​e對OP有幫助? – dc5 2014-08-27 16:19:08

+0

@ dc5是的,也許有,好點。我剛剛更新了它 – 2014-08-27 16:20:43

+1

這是非常有用的回覆,我工作的是: var title = $(this).parent('span')。parent('div')。children('。title')。text( ); – user1034380 2014-08-27 16:49:28

0

像這樣每個變種:

var date = $('.movie').find('.date').text();