简体   繁体   English

jQuery click函数—选择器的css未被更改

[英]jQuery click function — selector's css not being changed

I'm going nuts over an issue I'm sure is obvious to someone. 我要为一个肯定对某人显而易见的问题发疯。

I want a certain section of my page to disappear when a button is clicked. 我希望页面的某个部分在单击按钮时消失。

Here's my code: 这是我的代码:

$(document).ready(function(){
   $('.button').click(function() {
        $('.page-section').css('display', 'none');
   });
});

The script is being referenced correctly. 脚本已正确引用。 jQuery is being loaded first. jQuery首先被加载。 If I put an alert('something') inside the click event, it fires. 如果我在click事件中放置了一个Alert(“某物”),则会触发该事件。

But for some reason, I am unable to get the css to change. 但是由于某种原因,我无法更改CSS。

When I use the same line of code in my console, the css changes as it should. 当我在控制台中使用同一行代码时,css会发生应有的变化。

Any ideas? 有任何想法吗?

Try replace this: 尝试替换此:

 $('.page-section').css('display', 'none');

with this: 有了这个:

 $('.page-section').css({'display':'none'});

or 要么

 $('.page-section').hide();

Hope it helps. 希望能帮助到你。

Your code seems like it should work. 您的代码似乎应该可以工作。 Have you inspected that element with developer tools after clicking the button? 单击按钮后,您是否已使用开发人员工具检查了该元素? You should be able to see what classes are being applied, even if another style is overriding it, for example, if you had .page-section{display:block !important;} somewhere in your stylesheet, your jQuery wouldn't hide it. 即使另一种样式覆盖了它,您也应该能够看到正在应用哪些类,例如,如果样式表中的某处有.page-section {display:block!important;},则jQuery不会将其隐藏。 It also seems possible that that element is actually being hidden, but it's actually some other element you're trying to hide. 该元素似乎实际上已被隐藏,但实际上您正在尝试隐藏其他元素。

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

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