更改具有相同颜色的元素的背景颜色
我想在这里创建一个简单的主题系统。我有几个带有红色背景颜色的元素。我还有一个按钮可以将元素背景颜色更改为绿色。
我正在尝试编码,但我不知道如何选择所有红色元素的背景颜色并将其更改为绿色!
例如,我有 4 个 div。其中两个有红色标题,当我单击按钮时,这些标题背景颜色必须更改为绿色。这里没问题,但问题是div是动态生成的,所以我必须循环所有页面来找到红色背景颜色吗?有人可以启发我吗? =)
非常感谢! =)
I'm trying to create a simple theme system here. I have few elements with red background color. I also have a button that will change the elements background color to green.
I'm trying to code but I couldn't figure out how can I select and change the bg color of all red elements to green!
For example, I have 4 divs. Two of these have a red header, and when I click the button these headers background color must be changed to green. It's Ok here, but the problem is that the divs are dynamically generated, so do I have do loop all the page to find the red bg color? Could someone enlight me? =)
Thanks a lot! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为最好的解决方案是为每个主题使用不同的样式表,并将当前主题值保留在 SESSION 或 COOKIE 中,或者只是将其作为 URL 参数传递。但这将是服务器端解决方案。
在客户端,您可以为每个主题使用不同的类,并使用 .toggleClass() 在按钮推送事件上切换它们
I think the best solution would be to use different stylesheet for each theme and keep the current theme value in SESSION or COOKIE or just pass it as URL argument. But that would be a server side solution.
On Client side, you can just use different classes for each theme and toggle them on the button push event using .toggleClass()
最简单的方法是为您使用的每种背景颜色指定一个特定的 CSS 类。例如:
一旦你这样做了,你就可以选择 CSS 类并设置 CSS 类:
这是一种迂回的方法,但是没有简单的方法来选择 CSS 属性(要做到这一点,你必须选择页面的所有元素,然后检查每个元素的背景颜色属性,这效率非常低......)
The easiest way to do this would be to have a specific CSS class for each background color you're using. ex:
Once you do that you can select on the CSS class as well as set the CSS class:
It's kind of a round-about way of doing it, however there is no easy way to select on a CSS attribute (to do that you'd have to select all elements of the page and then check each one of them for the background color attribute which would be scarily inefficient...)
我不知道你所说的标题是什么意思,但我认为这应该可以满足你的要求:
I don't know what you mean by headers, but I think this should do what you want: