从DIV获取内部文本 [英] Get Inner Text from DIV
本文介绍了从DIV获取内部文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML:
<div id="country">
<span>B</span>
Bangladesh
</div>
<div id="capital">
Dhaka
<span>D</span>
</div>
- 我想从
#country
获得孟加拉国 - 我想从
#capital
获得达卡
- From
#country
, I want to get Bangladesh - From
#capital
, I want to get Dhaka
我该如何实现?
推荐答案
尝试一下是否可行
$('#country').clone().children().remove().end().text();
$('#capital').clone().children().remove().end().text();
.clone()
克隆所选元素.
.children()
从克隆的元素中选择子项
.children()
selects the children from the cloned element
.remove()
删除先前选择的子项
.end()
再次选择所选元素
.text()
从没有子元素的元素获取文本
.text()
gets the text from the element without children
这篇关于从DIV获取内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文