且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在类中使用JQuery更改CSS规则

更新时间:2023-11-10 11:17:40

jQuery.css 会找到在页面上具有 Foo 类的所有现有元素,然后设置其内联样式 width 40px

jQuery.css will find all existing elements on the page that have the Foo class, and then set their inline style width to 40px.

换句话说,更改css规则 - 如果你动态添加一个元素与 Foo 类,它仍然会有一个宽度 20px ,因为其内联样式未设置为覆盖默认CSS规则。

In other words, this doesn't create or change a css rule -- if you dynamically add an element with the Foo class, it would still have a width of 20px, because its inline style hasn't been set to override the default CSS rule.

而应使用 addClass removeClass 并控制静态CSS中的样式。

Instead, you should use addClass and removeClass and control the styles in your static CSS.