jQuery 如果 div 为空
我正在研究 WordPress 模板,其中 div.post-thumb 为空,但有动态空白。
<div class="post-thumb"> </div>
<div class="post-text"></div>
我正在使用以下脚本来检测 div.post-thumb 是否为空。
$('.post-thumb').each(function(){
if ($(this).html()=='') $('.post-text').css('marginLeft','50px');
});
这个脚本的问题是“div.post-thumb 内的动态空白”
如何摆脱这个问题?有没有其他方法来检测 div 是否为空(不包括空格)?
I'm working on wordpress template where div.post-thumb is empty but have dynamic white spaces.
<div class="post-thumb"> </div>
<div class="post-text"></div>
I'm using following script to detect if div.post-thumb is empty.
$('.post-thumb').each(function(){
if ($(this).html()=='') $('.post-text').css('marginLeft','50px');
});
Problem with this script is "Dynamic white spaces within div.post-thumb"
How to get rid of this issue? Is there any other way to detect if div is empty excluding white spaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 jQuery
trim
函数删除空格:trim
删除字符串开头或结尾的所有空格、换行符和制表符。有关详细信息,请参阅 jQuery 文档。You can use the jQuery
trim
function to remove whitespace:trim
removes all spaces, new lines and tabs from the beginning or end of a string. See the jQuery docs for more information.