jQuery如何在div中选择所有节点文本

时间:2014-08-27 16:11:35

标签: javascript 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 个答案:

答案 0 :(得分:2)

尝试以获取每个值:

$('.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/

答案 1 :(得分:0)

每个var都是这样的:

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