简体   繁体   中英

get the value of span inside a div jquery

How do I get the whole segment of Location Address including the address, city state and ZIP inside of a span which is inside another div in jquery?

<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>

Try the following

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

Note: If there is more than one set of DOM elements which matches that selector then the text returned will be the combined contents of all of them.

How about?

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

It doesn't matter where it's at in the DOM tree, unless you actually want to limit its scope to one parent or another. If you want to preserve the <br/> as well, use .html() instead of .text() .

UPDATE

So, in truth, you most likely want each .EachLocation 's address and not just the first. So it should really be something like:

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

最简单的方法是将id赋予span ,然后获取valuehtml所需要的value ,例如,如果span id设置为locationaddress

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

使用JQuery:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM