如何在if语句中的克隆元素上使用`$ .trim()`?

时间:2014-10-18 13:01:04

标签: jquery

我正在尝试检查if a paragraph (excluding any nested child tags) contain text - 但是现在它的缩进发出误报 - 那么如何使用$.trim()来摆脱它们呢?

http://jsfiddle.net/frank_o/td8t4gpv/2/

HTML:

<div>
    <p>
      <a>Should not react to this</a>
    </p>
</div>

JS:

if($('p').clone().children().remove().end().text()) {
  console.log('WRONG! Paragraph does not have any text');
}

1 个答案:

答案 0 :(得分:1)

将您的字符串传递给$.trim

if(! $.trim($('p').clone().children().remove().end().text())) {
    alert('WRONG! Paragraph does not have any text');
}

JSFiddle Demo