如何在没有javascript的情况下隐藏和显示文本
如何在不使用 javascript 的情况下隐藏和显示文本?我知道使用 javascript 会容易得多,但在这种情况下我不能。
How can you hide and show text without using javascript? I know using javascript would be much easier, but in this case I can't.
I'm looking for something like this: http://www.pmob.co.uk/temp/hideandshow3-css.htm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
老实说,不知道为什么有些人想成为代码势利小人,但你的方法没有任何问题。我必须为那些没有启用 JavaScript 的人编写代码,因此利用 CSS 是非常有意义的……利用它的潜力并不是一种黑客行为。
这是一篇出色的文章,可以完全满足您的要求:
https://www.cssportal.com/css3-preview/showing-and-hiding-content-with-pure-css3.php
希望对某人有所帮助!
Honestly don't know why some folks want to be code snobs, but there's nothing wrong with your approach. I have to code for folks that don't have javascript enabled, so it makes perfect sense to leverage CSS...it's NOT a hack to use its potential.
Here is an excellent article to do exactly what you want:
https://www.cssportal.com/css3-preview/showing-and-hiding-content-with-pure-css3.php
Hope that helps someone!
您可以查看该页面上的代码以了解它是如何发生的:
但是,我不建议这样做,因为做这样的事情几乎是对 CSS 的修改。 CSS 应该用于设计元素的样式,以提供外观和感觉;不是像 javascript 那样提供元素的行为。
You can view the code on that page to see how it's happening:
However, I wouldn't recommend doing this because it's almost a hack of CSS to do things like this. CSS should be used for styling your elements to give a look and feel; NOT for providing behavior of elements like javascript is for.
它使用 a:active 属性来显示这些文本。
基本上,您需要将内容放入链接中,当链接不活动时隐藏它,但如果链接活动则显示。
问题是,单击任何链接都会隐藏当前显示的内容,并且选项卡链接将被计为“单击”。
使用它并不是一个好主意。使用 a:hover 会更好。
It use the a:active property to display those texts.
Basically, you need to put your content inside your link, hide it when the link is not active but show if it is active.
The issue is that clicking on any link will hide the currently shown content and tabbing links will be counted as "clicking".
It is not a really a good idea to use it. Using a:hover would be better.
您可以使用 CSS:
active 选择器可让 CSS 检测用户何时单击 HTML 元素。在此示例中,当单击该元素时,会将不透明度设置为 0,这意味着它是透明的。或者,如果您希望它真正隐藏,请执行以下操作:
这再次使用活动选择器。
You can use CSS:
The active selector lets CSS detect when the user clicks on an HTML element. In this example, when the element is clicked, it sets the opacity to 0, which means it's transparent. Or, if you want it to really hide, do this:
This again uses the active selector.