Jquery 查找不起作用
这是我的页面的 Html,我只想如果没有流行产品,那么整个 div populate_prd 应该隐藏。
<div class="popular_prd">
<h1 class="views-heading">Popular Products</h1>
<div class="view view-popular-products view-id-popular_products view-display-id-default boxes-bottom view-dom-id-3">
<div class="views-admin-links views-hide">
<ul class="links">
<li class="0 first">
<a href="/admin/build/views/edit/popular_products?destination=node#views-tab-default">Edit</a>
</li>
<li class="1">
<a href="/admin/build/views/export/popular_products">Export</a>
</li>
<li class="2 last">
<a href="/admin/build/views/clone/popular_products">Clone</a>
</li>
</ul>
</div>
</div>
</div>
我使用下面的 jquery 代码来隐藏 div。
$('document').ready(function(){
if(!$('.popular_prd').find('.view-content') ) {
$('.popular_prd').hide();
}
else {
$('.popular_prd').show();
}
});
但代码不起作用,div 仍然显示。
This is the Html for my page I just want if there are no popular products then the whole div popular_prd should hide.
<div class="popular_prd">
<h1 class="views-heading">Popular Products</h1>
<div class="view view-popular-products view-id-popular_products view-display-id-default boxes-bottom view-dom-id-3">
<div class="views-admin-links views-hide">
<ul class="links">
<li class="0 first">
<a href="/admin/build/views/edit/popular_products?destination=node#views-tab-default">Edit</a>
</li>
<li class="1">
<a href="/admin/build/views/export/popular_products">Export</a>
</li>
<li class="2 last">
<a href="/admin/build/views/clone/popular_products">Clone</a>
</li>
</ul>
</div>
</div>
</div>
I used the following jquery code to hide tthe div.
$('document').ready(function(){
if(!$('.popular_prd').find('.view-content') ) {
$('.popular_prd').hide();
}
else {
$('.popular_prd').show();
}
});
But the code is not working the div is still showing up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这只会返回一个永远不会计算为 false 的 jQuery 对象
,您需要检查它的长度
This will just return a jQuery object which never evaluates to false
You need to check the length of it
find
并不像您想象的那样工作。它看起来像而不是你的意思是
事实上,你甚至不需要在这里查找...
底线,jQuery 选择器不返回 true 或 false,而是一个集合。测试
长度
以查看是否找到任何内容。find
doesn't work the way you think. It looks like instead ofwhat you mean is
In fact, you don't even need
find
here...Bottom line, jQuery selectors don't return true or false, but a set. Test the
length
to see whether anything was found.或者使用
length
属性Or use
length
property您的 HTML 中没有 view-content 类。
You don't have a view-content class in your HTML.
上述
HTML
中view-content
类未包含在popular_prd
类的 div 中view-content
class is not contained in the div with classpopular_prd
in the aboveHTML