Can we make onclick effect in css like we have :hover? [duplicate]

心已入冬 提交于 2019-12-13 07:46:28

问题


I'm making a Accordin for a site when i will click on + icon then another div will show. i'm using jquery for this. but is this possible in css to open a div on click on another element


回答1:


With strictly CSS, no this isn't possible. With jQuery:

$("#idOfPlus").click(function() { 
  $("#myPlusDiv").show();
});



回答2:


It is not possible using plain CSS. You can opt for any javascript framework.




回答3:


Yes it is possible with the focus pseudo-class:

<html>
<head>
<style type="text/css">
#hidden { display: none; border: 2px solid green; cursor: default; }
a {text-decoration: none;}    /* unvisited link */
a, div:visited { color: blue; }
a:focus #hidden { color: black; display: block; }
</style>
</head>

<body>
<a href="#">Click to show box
<div id="hidden">More information!<br/>Click outside to remove</div>
</a>
</body>
</html>

Even though this does (kinda) what you want it is not usable since your box is in the link and thus clickable. And when clicking outside the box, it is closed. So unless this is not a problem to you, I have to say it seems kinda impossible for me as well...

(Unless someone else is better than me in using CSS and can think of something more clever)

Edit: If you find another HTML element that supports the focus pseudo-class, it might be better than using an <a> element as my example does.




回答4:


You can make the icon an anchor that points to itself or nothing (href="#") and then use :active to trigger the class change. But the only catch is that as soon as they are not clicking, it will return to the non-active state, so your accordion would close as fast as it opened unless you had another rule to hold the class.



来源:https://stackoverflow.com/questions/2167994/can-we-make-onclick-effect-in-css-like-we-have-hover

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!