JQuery hide Class如果其他类可见或显示 [英] JQuery hide Class if other class is visible or shown

查看:131
本文介绍了JQuery hide Class如果其他类可见或显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找到类似的问题,但没有什么涵盖我需要的。我在我的例子中保持简单,我想使用JQuery。

Found similar questions but nothing that covers exactly what I need. I kept it simple in my example and I want to use JQuery.

我有两个类。如果类别div在网页加载时显示,我想隐藏过滤器div。目前没有与任一类相关联的样式。我相信我很接近,但它不工作。

I have two classes. I want to hide the "filter" div if the "category" div is shown on page load. There are currently NO styles associated with either class. I believe I am pretty close but it doesn't work.

<script language="text/javascript">
if(!$(this).hasClass("category")){
$('filter').css('display', 'none');
}
</script>

<div class="category">By Category</div>
<div class="filter">By Custom Filter</div>

提前感谢!

推荐答案

使用 .length 测试元素是否存在。

Use .length to test if an element is present.

使用 .hide()& .show()可显示和隐藏元素。

Use .hide() & .show() to show and hide elements.

最后,页面已完成加载,因此您要将其全部封装在 $(document).ready()

And finally, you want the code to run only when the page has finished loading, so you want to wrap it all in $(document).ready().

所以这样的东西应该工作最好:

So something like this should work best:

<script language="text/javascript">
    $(document).ready(function() {
        //following code will hide all elements with a class of 'filter'
        //if any elements with a class of 'category' are found
        if($('.category').length){
            $('.filter').hide();
        }
    });
</script>

<div class="category">By Category</div>
<div class="filter">By Custom Filter</div>

HTHs,

Charles

HTHs,
Charles

这篇关于JQuery hide Class如果其他类可见或显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆