2014-10-18 43 views
2

我试图检查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'); 
} 
+0

你想修剪一个tage tex里面的所有东西吗? – 2014-10-18 13:04:28

+0

本质上'.text()'应该被应用到'$('p')'的修剪版本,即。 '

...

'。 – 2014-10-18 13:05:35

回答

1

通过你的字符串$.trim

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

JSFiddle Demo

+0

非常好 - 非常感谢! – 2014-10-18 13:09:50