简体   繁体   English

jQuery hide() 方法使用 display:none !important 显示元素

[英]jQuery hide() method do show element with display:none !important

I have assigned an element a class which has following CSS:我为一个元素分配了一个具有以下 CSS 的类:

.cls {
  display:none !important;
}

When I try to show this element with jQuery当我尝试用 jQuery 显示这个元素时

$(".cls").show();

It does not work.它不起作用。

How can I hide this element?我怎样才能隐藏这个元素?

$('.cls').attr('style','display:block !important');

演示

Although question has been asked long back but its still relevant for new coders/beginners.虽然很久以前就有人问过这个问题,但它仍然与新的编码员/初学者相关。 Generally this situation comes when you have already applied some class which overriding the display behavior using !important property.通常,当您已经应用了一些使用!important属性覆盖display行为的类时,就会出现这种情况。

Other answers are also relevant to the question and its a matter of achieving the same goal with different approach.其他答案也与该问题相关,这是用不同方法实现相同目标的问题。 I would recommend to achieve it using already available classes in the same library (bootstrap here), instead of writing custom class as these days when most of us are using Bootstrap classes to build the layout.我建议使用同一个库中已经可用的类(这里是引导程序)来实现它,而不是像现在我们大多数人使用引导程序类来构建布局那样编写自定义类。

<div id='container' class="d-flex flex-row align-items-center">
.....
</div>

If you see in the above code, we are using d-flex class for setting display property of this container.如果您在上面的代码中看到,我们正在使用d-flex类来设置此容器的显示属性。 Now if I am trying to show/hide this container using现在,如果我尝试使用show/hide此容器

$('#container').show()

or

$('#container').hide()

It will not work as per expectation because of它不会按预期工作,因为

  1. As d-flex is already using !important property由于d-flex已经在使用!important属性
  2. Jquery's show() method will add display:block not display:flex to the css property of container. Jquery 的show()方法将display:block而不是display:flex到容器的 css 属性。

So I will recommend to use hidden class here.所以我会推荐在这里使用hidden类。

To show container显示容器

$('#container').removeClass('hidden')

To hide container隐藏容器

$('#container').addClass('hidden')

2 ways of doing this, 2种方法,

1) Remove the !important from your .cls class, 1) 从.cls类中删除!important

.cls{
   display: none;
}

But I assume, you'd have used this elsewhere so it might cause regression.但我认为,你会在其他地方使用它,所以它可能会导致回归。

2) What you could alternatively do is, have a another class and toggle that, 2)你可以做的是,有一个另一个类并切换它,

.cls-show{
  display: block !important;
}

And then in your javascript,然后在你的 javascript 中,

$('.cls').addClass(".cls-show");

Then when you need to hide it again, you can,然后当你需要再次隐藏它时,你可以,

$('.cls').removeClass('.cls-show');

This will help you keep your markup clean and readable这将帮助您保持标记的清洁和可读性

!important; !重要; remove all rules and apply the css desfined as !important;删除所有规则并应用定义为!important的 css . . So in your case it is ignoring all rules and applying display:none .因此,在您的情况下,它会忽略所有规则并应用display:none

So do this:所以这样做:

.cls {
  display:none
}

See this also这也

If the only property in the CLS class selector is the display one, you can do this and don't need to add any extra classes or modify the inline style.如果 CLS 类选择器中唯一的属性是显示属性,则可以执行此操作,而无需添加任何额外的类或修改内联样式。

To show them:向他们展示:

$('.cls').removeClass("cls").addClass("_cls");

To hide them:要隐藏它们:

$('._cls').removeClass("_cls").addClass("cls");

Just had this exact issue, here's what I did first, I added another class to the element, such as:刚刚有这个确切的问题,这是我首先做的,我向元素添加了另一个类,例如:

<div class="ui-cls cls">...</div>

Then in the javascript:然后在javascript中:

$('.ui-cls').removeClass('cls').show();

The nice thing is that you can also have this code to hide it again:好消息是您还可以使用此代码再次隐藏它:

$('.ui-cls').hide();

and it doesn't matter how many times you hide/show, it'll still work不管你隐藏/显示多少次,它仍然有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM