.p元素的含义。只降低一级......需要所有内容。 jQuery的

时间:2011-06-21 23:14:58

标签: jquery

有问题。拿这个HTML:

 <p><span>js fiddle is a playground for
 web developers, a tool which may be
 used in many ways. One can use it as
 an online editor for snippets build
 from HTML, CSS and JavaScript. The
 code can then be shared with others,
 embedded on a blog, etc. Using this
 approach, JavaScript developers can
 very easily isolate bugs. We aim to
 support all actively developed
 frameworks - it helps with testing
 compatibility.</span></p>

我运行此代码:

var textNodes = $('p').contents().filter(function() {
    return this.nodeType == 3 && this.nodeValue.length > 150;
}).parent().parent();

$(textNodes).css('border', '2px solid black');

我遇到的问题是contents()只降低了一级...我想要的是测试p元素是否包含超过150个nodeType == 3个字符。如果我这样做contents().contents()....

2 个答案:

答案 0 :(得分:1)

使用$.text

var textNodes = $('p').filter(function() {
    return $(this).text().length > 150;
}).parent();

textNodes.css('border', '2px solid black');

答案 1 :(得分:0)

您可以检查每个text元素的p属性的长度。如果大于150,请更改CSS。查看示例小提琴here