获取嵌套在许多类中的值

时间:2015-02-25 10:12:58

标签: javascript jquery css

我试图在pickup-address课程中获得价值,但收效甚微。到目前为止这是我的代码。谁能告诉我我做错了什么?

JS

var pickupAddress = $('.timeline-item.active-item > .timeline-status > .pickup-address').val();
alert(pickupAddress);

HTML

<div class="timeline-item active-item" style="background-color: rgb(255, 255, 255);">
    <img src="https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg" alt="" class="img-rounded profile">
    <div class="timeline-status" style="width: 185px;"><strong>Stephen
    </strong>
        <br><span class="pickup-address">Whaddon</span> — <span class="dropoff-address">Bancroft</span>
        <br><span class="timeline-substatus grey-status waypoints">Via Lathbury</span>
        <br><span class="timeline-substatus grey-status remaining-seats">3 places remaining</span>
    </div>
</div>

2 个答案:

答案 0 :(得分:3)

使用.text.html代替.val,因为pickup-address不是input字段

var pickupAddress = $('.timeline-item.active-item > .timeline-status > .pickup-address').text();

Example

答案 1 :(得分:2)

使用text()而不是val()。我认为val()仅适用于输入元素。