我正在尝试检查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 :(得分:1)
将您的字符串传递给$.trim
:
if(! $.trim($('p').clone().children().remove().end().text())) {
alert('WRONG! Paragraph does not have any text');
}