javascript 在 div jquery 中获取 span 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8807797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:38:44  来源:igfitidea点击:

get the value of span inside a div jquery

javascriptjquerytexthtml

提问by Anju Thapa

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 内的跨度内的地址、城市状态和 ZIP?

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

回答by JaredPar

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 元素与该选择器匹配,则返回的文本将是所有元素的组合内容。

回答by Chris Pratt

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().

它在 DOM 树中的哪个位置并不重要,除非您确实想将其范围限制为一个或另一个父级。如果您还想保留<br/>,请使用.html()代替.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人的地址,而不仅仅是第一个。所以它真的应该是这样的:

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

回答by Nate B

With JQuery:

使用 JQuery:

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

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

回答by Habib Rehman

The easiest way to do is to give idto the spanand then get the valueor htmlwhat ever is required for example if span idis set to locationaddress

做最简单的方法是给idspan再拿到valuehtml曾经需要什么,例如,如果跨度id设为locationaddress

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