开发者

How to grab content

I have such HTML node:

<div id='parent'>
   <a href="#">test</a>
   Need this
</div>

How to get 'Need this' text if I have handle for p开发者_开发问答arent object? I tried something like: $('#parent').text() but it also returns 'test'.


p.s. Nothing about editing html! I just have such content and I need to parse it.


var temp = $('#parent').clone();
temp.children().remove();
alert(temp.text());

working example: http://jsfiddle.net/hunter/H6jQp/


If you know that the text content is always the last content in the div, you can do:

$('#parent').contents().last().text();

on this markup:

<div id='parent'>
   <a href="#">test</a>
   Need this
</div>

Returns: Need this

Sample: http://jsfiddle.net/64RVf/


this should do it:

$('a').click(function(){
   alert($(this).parent().clone().find('a').remove().end().text());
});

here's a fiddle: http://jsfiddle.net/RTqyy/

probably not the best way, i'd wrap that text in a div or span or p. but this will work.


var $text = $("#parent").contents().filter(function(){ return this.nodeType != 1; });
alert($text.text());

Seems to do the trick. (jsFiddle)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜