如果其他类可见或显示,JQuery 隐藏类

发布于 2024-10-05 10:28:27 字数 412 浏览 0 评论 0原文

发现类似的问题,但没有什么能完全满足我的需要。我在示例中保持简单,并且我想使用 JQuery。

我有两节课。如果页面加载时显示“类别”div,我想隐藏“过滤器”div。目前没有与这两个类别相关的样式。我相信我已经很接近了,但它不起作用。

<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>

提前致谢!

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

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>

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沉鱼一梦 2024-10-12 10:28:27

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

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

最后,您希望代码仅在页面加载完成时运行,因此您希望将其全部包装在 $(document).ready() 中。

所以像这样的东西应该最有效:

<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,
查尔斯

Use .length to test if an element is present.

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

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

枕梦 2024-10-12 10:28:27

将选择器更改为 $('.filter')

Change the selector to $('.filter')

别把无礼当个性 2024-10-12 10:28:27
var $filter = $(".filter");
var $category = $(".category");

$category.is(":visible") ? $filter.hide() : $filter.show();
var $filter = $(".filter");
var $category = $(".category");

$category.is(":visible") ? $filter.hide() : $filter.show();
策马西风 2024-10-12 10:28:27

jQuery

$('div').each(function() {
    if ($(this).hasClass("category")) {
        $('.filter').css('display', 'none');
    }
});

或者只是:

if ($('.category').length) {
    $('.filter').css('display', 'none');
}

jQuery

$('div').each(function() {
    if ($(this).hasClass("category")) {
        $('.filter').css('display', 'none');
    }
});

or just:

if ($('.category').length) {
    $('.filter').css('display', 'none');
}
小鸟爱天空丶 2024-10-12 10:28:27
 <div class="category">By Category</div>
 <div class="filter">By Custom Filter</div>

假设“在页面加载时显示”是指渲染而不是显示/隐藏可见性。

$('.category').each(function(){
    $('.filter').hide();
});
 <div class="category">By Category</div>
 <div class="filter">By Custom Filter</div>

Assuming by "shown on page load" you mean rendered and not the show/hide visibility.

$('.category').each(function(){
    $('.filter').hide();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文