更新 CSS 规则属性值
我有一个 html 元素,它的样式(使用 jquery)带有通过其类名定位的背景图像。
当我删除该类时,背景图像仍然存在 - 这不是我所期望或想要的。
test.html
<div id='log' class='tile'>HELLOWORLD</div>
test.css
.tile{
background: none;
}
test.js
$('.tile').css("background-image", "url(tile.jpg)"); // We see image
$('#log').toggleClass('tile'); // We still see image
在敲了敲我的头之后,我想我知道发生了什么。 css 应用于元素 - 而不是“类”。
如何定位特定的 CSS 规则以便更新其键值?
如果这是有道理的话。
I have a html element which is styled (using jquery) with a background image targeted thru its class name.
When I remove the class the background image stays - which is not what I expected or want.
test.html
<div id='log' class='tile'>HELLOWORLD</div>
test.css
.tile{
background: none;
}
test.js
$('.tile').css("background-image", "url(tile.jpg)"); // We see image
$('#log').toggleClass('tile'); // We still see image
After banging my head I think I know whats happening. The css is being applied to the element - NOT to the 'class'.
How can I target a specific css rule so that its key values can be updated?
If that makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果你想改变“.tile”类的CSS规则,那么你可以这样做。
有一篇文章解释得非常清楚好吧:
你可以这样称呼它:
If you wan to change the css rules of the ".tile" class, then you can do it.
There is a post that explains it very well :
You can call it like this :
问题是您将
background-image
设置为覆盖任何样式表规则的内联样式。切换类不会有任何影响。您可以通过样式表规则设置背景,然后添加一个类来删除它;
或者您可以只使用
!important
作为;...可能是相反的情况,但你明白了吗? :)
The problem is that you´re setting the
background-image
as an inline stlye that overrides any stylesheet rules. Toggling the class won´t have any affect.You can either have set the background through a styleheet rule and then add a class that removes it;
or you could just use
!important
as;...it might be the other way around but you get the point? :)
尝试删除类
tile
并使用 bg 应用新类:无效果 - 需要时使用 bg 应用类,不需要时 - 不使用
try removing class
tile
and applying new class with bg: nonein effect - when needed apply class with bg, when not needed - without
在这种情况下不需要 jQuery。您可以使用普通的旧 JavaScript。查看本教程:
javascriptkit.com - 使用 DOM 更改外部样式表
No need for jQuery in this case. You can use plain old JavaScript. Check out this tutorial:
javascriptkit.com - Changing external style sheets using the DOM
如果不重新编写样式表中的声明,则无法更改类本身,您只能使用选择器中的元素。
尝试:
You can't change the class itself without re-writing that declaration in the stylesheet, you ARE working only with the element in the selector.
Try:
我会将背景图像作为 css 样式作为类的一部分:
然后在必要时使用 jquery 删除该类
I would make the background image part of the class as a css style:
and then remove the class when necessary with jquery
你可以在你的css中有两个类...
然后使用jquery只需切换这些类...
我确信这只是执行此操作的多种方法之一。我希望它有帮助。
you could have two classes in your css...
and then with jquery just toggle the classes...
I'm sure this is just one of many ways of doing this. I hope it helps.
你们非常接近。
看起来您正在向元素添加内联 CSS,然后尝试切换该类。在大多数情况下,您应该将CSS样式分开:
HTML:
jQuery(我想这应该在单击或其他事件时完成):
如果“tile”类已经写入HTML,则切换它将删除它。
CSS:
You are very close.
It seems like you are adding inline CSS to your element and then trying to toggle the class. You should keep CSS styling separate in most cases:
HTML:
jQuery (I imagine this should be done on click or another event):
If the "tile" class is already written to the HTML, then toggle-ing it will remove it.
CSS: