简体   繁体   English

单击<a>元素</a>时应用新的CSS规则

[英]Apply new CSS rules when on click of <a> element

I'm currently trying to design and develop my own lightweight lightbox. 我目前正在尝试设计和开发自己的轻型灯箱。 The idea is when someone clicks <a href="#tab1"> it will change the properties of the <div id="tab1"> from display: none to display: block . 这个想法是当某人单击<a href="#tab1">它会将<div id="tab1">的属性从display: none更改为display: block Once the lightbox pops up it will have another link like <a href="#tab1_close"> to reverse the CSS properties back to display: none . 灯箱弹出后,它将具有另一个链接,例如<a href="#tab1_close">以将CSS属性反向display: none

I've used input[name='name'] ~ #id for a lot of other sections on my site, I'm wondering if it's possible to change it so the input recognizes the <a> as the input and applies the CSS changes on the click. 我在网站上的许多其他部分中使用了input[name='name'] ~ #id ,我想知道是否可以更改它,以便输入将<a>识别为输入并应用CSS点击变化。

Ideally, I could add id="tab_1" to the <a> , then the code would be something like a#tab_1:active ~ #tab1 { display: block } but that code doesn't seem to do anything. 理想情况下,我可以在<a>添加id="tab_1" ,然后代码将类似于a#tab_1:active ~ #tab1 { display: block }但是该代码似乎没有任何作用。

Here's my Code Pen 这是我的密码笔

If you want CSS only try the :target selector: 如果只需要CSS,请尝试:target选择器:

#tab1:target {
  display: block;
}

http://codepen.io/anon/pen/okEqr http://codepen.io/anon/pen/okEqr

jQuery的toggleClass()将添加您想要的帽子: https : //api.jquery.com/toggleClass/

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

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