0

Possible Duplicate:
Getting text within element excluding decendants

how to get only the text of an element without getting the inline element using jQuery?

I have the following problem:

<p><strong>Test 1</strong> this is test 1 results</p>

How do I capture only "this is test 1 results" into an array using jQuery? I attempted this but it's not working:

var TextResults = $("strong").parent("p").text();
var arrayTestResults = TextResults.split(" ");
4

1 回答 1

0

您可以使用 contents.map() 函数(如 Felix Kling 提供的链接中所建议的那样)来执行此操作,如下所示:

var result = $('p').contents().map(function() {
    if( this.nodeType === 3 ) {
        return this.data;
    }
   }).get().join('');

这是完整的例子,http://jsfiddle.net/z3Khs/3/

于 2012-09-30T20:42:39.897 回答