简体   繁体   English

获取div jQuery中span的值

[英]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? 我如何在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>

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. 注意:如果有多个与该选择器匹配的DOM元素,则返回的文本将是所有元素的组合内容。

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. 它在DOM树中的位置无关紧要,除非您实际上想将其范围限制为一个父对象或另一个父对象。 If you want to preserve the <br/> as well, use .html() instead of .text() . 如果还要保留<br/> ,请使用.html()而不是.text()

UPDATE 更新

So, in truth, you most likely want each .EachLocation 's address and not just the first. 因此,实际上,您很可能需要每个.EachLocation的地址,而不仅仅是第一个。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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