开发者

Use jQuery to select text not in an element

开发者 https://www.devze.com 2023-03-13 21:49 出处:网络
Say i have this: <div class=\'myDiv\'> <p>hello</p> hello <p>Hello</p> <开发者_JS百科;/div>

Say i have this:

<div class='myDiv'>
    <p>hello</p>
    hello
    <p>Hello</p>
<开发者_JS百科;/div>

How does one grab the text hello between the two P tags using jQuery?


$('.myDiv')
  .contents()
  .filter(function() {
    return this.nodeType == Node.TEXT_NODE;
  }).text();

How do I select text nodes with jQuery?

http://jsfiddle.net/6us8r/


js1568 has a better approach

$('div.myDiv').filter('p').text() would probably work.

I take it back, filter would not work. maybe something like:

var jText = $('div.myDiv').clone();
jText.find('p').remove();
jText.text();
0

精彩评论

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