如何使用 jQuery 添加和删除活动类?
我有一个无序列表。
<style>
#button1 { position:relative; z-index: 3; }
#button2 { position:relative; z-index: 2; }
#button3 { position:relative; z-index: 1; }
</style>
<ul>
<li id="button1">Button 1</li>
<li id="button2">Button 2</li>
<li id="button3">Button 3</li>
</ul>
我目前正在使用 css 为每个 id 提供不同的 z-index,以便一个按钮稍微位于下一个按钮之上。我想要做的是使用 jQuery 添加一个类,为单击的任何按钮(例如“999”)添加更高的 z-index,以确保它将位于其他两个按钮之上。我不知道如何将活动类添加到当前单击的
,同时从当前设置的任何按钮中删除活动类。我不知道如何让 jQuery 找出其他两个
另外,我不确定添加类是否可以解决问题,因为我已经通过针对
的 id 设置了 z-index。也许我需要影响当前按下按钮的内联 css,以便它覆盖 id 的 css,并检查其他两个中哪一个的内联 css z-index 设置为“999”,以便它可以删除它们内联 css 并具有比其他两个按钮更大的 z-index 并显示在顶部?I have an unordered list.
<style>
#button1 { position:relative; z-index: 3; }
#button2 { position:relative; z-index: 2; }
#button3 { position:relative; z-index: 1; }
</style>
<ul>
<li id="button1">Button 1</li>
<li id="button2">Button 2</li>
<li id="button3">Button 3</li>
</ul>
I am currently using css to give each id a different z-index so that one button is slightly on top of the next. What I want to do is to use jQuery to add a class that add's a much higher z-index to whatever button was clicked such as '999' to ensure that it will be on top of the other two buttons. I can't figure out how to add an active class to the currently clicked <li>
while removing the active class from whatever button currently has it set. I don't know how to make jQuery figure out which of the other two <li>
's has the active class set before the new button is pushed. How would I go about doing that?
Also I am not sure if adding a class will solve the problem since I am already setting the z-index by targeting the <li>
's id. Maybe I would need to affect the inline css for the currently pushed button so it overrides the id's css, and also have it check to see which of the other two has the inline css z-index set to '999' so it can remove their inline css and have a larger z-index than the other two buttons and appear on top?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
让科比的回答更进一步。如果可能的话,我会给 li(s) 一些公共类或者 ul 上的 id。
如果您根据 ID 设置 z-index,则 ID 的 css 规则可能会优先于任何基于类的规则。您也可以将 z-index 属性内联并使用 jQuery 更改它。
To take Kobi's answer a little bit further. I would give the li(s) some common class if possible or an id on the ul.
If you are setting the z-index based on IDs, then the css rule for the ID will likely take precedence over any class based rule. You could as well put the z-index property inline and change it with jQuery.
最佳实践是使用类,但您也可以修改样式属性,例如
the best practice is to use class, but you can also modify the style property, like
如果我理解正确,应该是
编辑 - 您遇到的是 CSS 问题 -
#id
规则优先于.class
规则。要回答您的问题,您可以使用以下方法设置 z-index:
if I understand correctly, should be
Edit - what you're experiencing is a CSS problem -
#id
rules take precedence to.class
rules.To answer your question, you can set z-index using:
通过查看所有答案并找到兄弟姐妹选择器,我找到了一个简单且适合我的答案。除了我没有使用类之外,请告诉我是否还有其他问题。我认为对于我的情况来说,效果很好!感谢大家的帮助!
由于 smack0007 的答案对我获得我正在使用的答案帮助最大,因此我给他/她打勾。
By looking at all you answers and upon finding the siblings selector, I figured out an answer that is simple and works for me. Please tell me if there is anything wrong with it other than I didn't use classes. I think for my situation, it works nicely! Thanks for all your guys' help!
Since smack0007's answer helped me the most to get the answer I am using, I am giving him/her the checkmark.
这是另一种方法...它存储 z-index 并在不是活动按钮时替换它。
Here is another method... this stores the z-index and replaces it when not the active button.