I want to extract the Text inside a Element with JQuery
我想用JQuery提取Element中的Text
<div id="bla">
<span><strong>bla bla bla</strong>I want this text</span>
</div>
I want only the text "I want this text" without the strong-tag. How can I do that?
我只想要没有强标签的文字“我想要这个文字”。我怎样才能做到这一点?
4 个解决方案
#1
7
Try this...
尝试这个...
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
$("#bla span").contents().each(function(i) {
if(this.nodeName == "#text") alert(this.textContent);
});
});
//]]>
</script>
This doesn't need to remove any other nodes from context, and will just give you the text node(s) on each iteration.
这不需要从上下文中删除任何其他节点,并且只会在每次迭代时为您提供文本节点。
#2
5
This does it (tested):
这样做(测试):
var clone = $("#bla > span").clone();
clone.find('strong').remove();
alert(clone.text());
#3
4
Variation on karim79's method:
karim79方法的变化:
$("span").clone().find("strong").remove().end().text();
or if you just want the root text without knowing what other tags are in it:
或者如果您只是想要根文本而不知道其他标签是什么:
$("span").clone().children().remove().end().text();
still a little long, but I think this about as short as you can hope for.
还有点长,但我认为这只是你希望的那么短。
#4
0
var textFromSpanWithoutStrongTag = $('div#bla > span').text();
UPDATED:
更新:
var textFromSpanWithoutTextFromStrongTag =
$('div#bla > span').text().replace($('div#bla > span > strong').text(), '');
UPDATED:
更新:
var text = '';
$("div#bla > span").contents().each(function () {
if(this.nodeType == 3) text = text + this.nodeValue; });
Tested in FF3, IE8, O10b on:
在FF3,IE8,O10b上测试:
<div id="bla">
<span>
<strong>bla bla bla</strong>
I want this
<strong>bla bla</strong>
text
<span>bla bla bla</span>
</span>
</div>
#1
7
Try this...
尝试这个...
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
$("#bla span").contents().each(function(i) {
if(this.nodeName == "#text") alert(this.textContent);
});
});
//]]>
</script>
This doesn't need to remove any other nodes from context, and will just give you the text node(s) on each iteration.
这不需要从上下文中删除任何其他节点,并且只会在每次迭代时为您提供文本节点。
#2
5
This does it (tested):
这样做(测试):
var clone = $("#bla > span").clone();
clone.find('strong').remove();
alert(clone.text());
#3
4
Variation on karim79's method:
karim79方法的变化:
$("span").clone().find("strong").remove().end().text();
or if you just want the root text without knowing what other tags are in it:
或者如果您只是想要根文本而不知道其他标签是什么:
$("span").clone().children().remove().end().text();
still a little long, but I think this about as short as you can hope for.
还有点长,但我认为这只是你希望的那么短。
#4
0
var textFromSpanWithoutStrongTag = $('div#bla > span').text();
UPDATED:
更新:
var textFromSpanWithoutTextFromStrongTag =
$('div#bla > span').text().replace($('div#bla > span > strong').text(), '');
UPDATED:
更新:
var text = '';
$("div#bla > span").contents().each(function () {
if(this.nodeType == 3) text = text + this.nodeValue; });
Tested in FF3, IE8, O10b on:
在FF3,IE8,O10b上测试:
<div id="bla">
<span>
<strong>bla bla bla</strong>
I want this
<strong>bla bla</strong>
text
<span>bla bla bla</span>
</span>
</div>