簡體   English   中英

獲取div jQuery中span的值

[英]get the value of span inside a div jquery

我如何在jquery的另一個div內的跨度內獲取位置地址的整個片段,包括地址,城市州和郵政編碼?

<div class="EachLocation" style="width:225px;float:right;position:relative; border-bottom:1px dotted silver;">
    Munster Women's Center<br />
    <span class="LocationAddress">1455 Sycamore Blvd.<br>Munster,IN 42103</span>
    <br>(219) 232-7601<br><br>
</div>

嘗試以下

var text = $('div.EachLocation span.LocationAddress').text();

注意:如果有多個與該選擇器匹配的DOM元素,則返回的文本將是所有元素的組合內容。

怎么樣?

$('.LocationAddress').text();

它在DOM樹中的位置無關緊要,除非您實際上想將其范圍限制為一個父對象或另一個父對象。 如果還要保留<br/> ,請使用.html()而不是.text()

更新

因此,實際上,您很可能需要每個.EachLocation的地址,而不僅僅是第一個。 所以它實際上應該是這樣的:

$('.EachLocation').each(function(){
    $('.LocationAddress', this).text();
});

最簡單的方法是將id賦予span ,然后獲取valuehtml所需要的value ,例如,如果span id設置為locationaddress

$('#locationaddress').html();

使用JQuery:

$('div.EachLocation span.LocationAddress').html()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM