有没有一种跨浏览器的方式来通过CSS扩展点击文本?
首先,这是我想做的:
Row 1
Row 2
Row 3
我有代码设置,以便当我将鼠标悬停在 row1、2 或 3 上时,该行会突出显示。 这是通过 CSS 完成的。
我希望能够(例如)单击 row1,然后它看起来像这样:
Row 1
Some text here
Row 2
Row 3
该文本将保留在那里,直到我单击另一行,此时它会消失。 例如,假设我接下来单击了第 2 行。
Row 1
Row 2
Even more text here
Row 3
我找到了一些代码,讨论使用 javascript 来执行此操作并设置文本的可见性,但我不确定如何在没有大量几乎重复的代码的情况下执行此操作...
First, here is what I would like to do:
Row 1
Row 2
Row 3
I have code setup so that when I hover over row1, 2 or 3, the row gets highlighted. This is done via css.
I would like to be able to (for instance) click row1 and then it would look like this:
Row 1
Some text here
Row 2
Row 3
That text would stay there until I clicked on a different row at which point it would go away. For instance, let's say I clicked on row 2 next.
Row 1
Row 2
Even more text here
Row 3
I have found code that talks about using javascript to do this and setting the visibility of the text but I'm not sure how I can do this without having a ton of near duplicate code...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的 javascript 多于此功能所需的量,那么 jQuery 是合理的,并且它会派上用场。 使用 jQuery,它会是这样的
If you have more javascript than the needed for this feature jQuery is justified and it will come handy. With jQuery, it would be something like
CSS:
HTML:
我认为这是在不使用 Javascript 的情况下所能达到的最接近的结果。
编辑:我想我可能误读了这个问题,我以为他不想使用Javascript。 哦,好吧,无论如何我都会留下我的答案。
CSS:
HTML:
I think that's as close as you can get without using Javascript.
Edit: I think I may have misread the question, I thought he didn't want to use Javascript. Oh well, I'll leave my answer up anyway.
处理此问题的唯一真正的跨浏览器方法是使用 Javascript,不幸的是,许多旧浏览器不支持除锚元素之外的任何内容上的 :hover 伪类。
您可能想查看 MooTools 的 Fx.Slide 效果,或者正如其他人总是提到的那样, jQuery.
The only real cross-browser way to handle this is with Javascript, as unfortunately many older browsers did not support :hover pseudo classes on anything other than anchor elements.
You might want to check out MooTools for their Fx.Slide effect, or as everyone else invariably mentioned, JQuery.
我可能也会使用 jQuery 来实现这一点,但由于 OP 根本没有提到这是一个普通的旧 JavaScript 解决方案,仅在 FF3/Mac 上进行了测试,但相当有信心它是跨浏览器的(如果我这样做,请纠正我)错了):
请注意,行内容仅在启用 JavaScript 时隐藏,这是一个重要的(且经常被忽略的!)可访问性方面。
I would probably just use jQuery for this as well, but since the OP didn't mention that at all here is a plain-old JavaScript solution, only tested on FF3/Mac but reasonably confident it's cross-browser (please correct me if I'm wrong):
Notice that the row content is only hidden when JavaScript is enabled, an important (and often missed!) accessibility aspect.
如果您想要一种简单的跨浏览器方式来执行此操作,我强烈建议您使用 jQuery
If you want an easy cross browser way of doing it, I would strongly advice you to use jQuery