gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:47:57 27 4
gpt4 key购买 nike

我正在尝试检查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');
}

最佳答案

将字符串传递给 $.trim:

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

JSFiddle Demo

关于jquery - 如何在 if 语句内的克隆元素上使用 `$.trim()` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439993/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com