可以使 a:after/before 伪元素作为链接的一部分可点击吗?
伪元素 a:after a:before 允许您添加看起来是链接一部分的文本。但是,我似乎无法找到一种方法来使该部分作为链接的一部分可点击。
例如,下面的 css 随后显示 url:
a:after {
content: " (" attr(href) ")";
}
...但它不可点击。
有人能在不改变底层 HTML 的情况下解决这个问题吗?
编辑:我使用的是 chrome 13.0.782.107。事实证明,这是一个错误。 (谢谢肖恩)
pseudo elements a:after a:before allow you to add text that appears to be part of the link. However, I can't seem to figure out a way to make that portion clickable as part of the link.
For example the following css shows the url afterward:
a:after {
content: " (" attr(href) ")";
}
...but it will not be clickable.
Anyone get around this without changing underlying HTML?
Edit: I am using chrome 13.0.782.107. It turns out it's a bug. (Thanks Sean)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您似乎在所使用的浏览器中发现了一个错误。
根据规范,生成的内容应被视为为其生成元素的子元素。我创建了一个 JSFiddle 来测试这一点,并且在大多数浏览器(Chrome 13 是唯一的例外。)我的猜测是您正在 Chrome 中进行测试。这是一个可重现的错误。
解决方法是简单地在链接上指定背景颜色...如果您希望能够将其用于所有链接,则声明背景图像(但不指定图像,或使用透明的
.gif - 或者正如刚才指出的,将
opacity
设置为 1) works< /a>.It looks like you have discovered a bug in the browser you are using.
Based on the spec, the generated content should be treated as a child of the element it is being generated for. I created a JSFiddle to test this out, and the generated text is linked for me in most browsers (Chrome 13 being the solitary exception.) My guess is that you are testing in Chrome. This is a reproducible bug.
The workaround is to simply specify a background color on your links ... if you want to be able to use this for all links, declaring a background image (but not specifying an image, or using a transparent
.gif
- or as just pointed out, settingopacity
to anything other than 1) works.如果您在 Wrapper 上有一个链接,那么您可以通过将
pointer-events
设置为 none 来使伪元素可点击。If you have a link on Wrapper then you can make pseudo-elements clickable by setting
pointer-events
to none.我遇到了同样的问题,显然如果我设置
(因此在链接本身上,而不是伪元素),它就可以工作。
I've had the same problem and apparently if I set
(thus on the link itself, not the pseudo element), it works.
我希望有人有比这更好的解决方案,但以防万一,我确实想出了这个可怕/蹩脚/黑客的解决方案:
I'm hoping someone has a better solution than this, but just in case not, I did come up with this horrible/crappy/hacky solution:
只需将其添加到您的 css 中:
如果 URL 的大小有所不同,您将必须使用 javascript 来获取它。
Just add this to your css:
If the size of the URL varies you will have to use javascript to get it.
为了避免修改文档树,您可以使用a:after
的 JavaScript 点击处理程序。编辑: 这不起作用,因为伪元素不会添加到 DOM 中。
To avoid modifying the document tree, you could use a JavaScript click handler fora:after
.Edit: This doesn't work, because pseudo elements aren't added to the DOM.
:before 和 :after 在选择器之前和之后添加内容。在 CSS 中,没有选择器可以让您获取和编辑标签内的内容。实现这一目标的唯一方法是使用 jQuery 或 javascript 来实际编辑 HTML。
The :before and :after add content before and after the selector. In CSS, there's no selector that let's you get and edit the content inside a tag. The only way to make that happen would be with jQuery or javascript to actually edit the HTML.
我将链接和文本分开包装——:before 放在容器上,链接放在里面。这样我就可以使用 :before 作为 jQuery tigger 并使用文本作为链接。
HTML:
CSS:
Jquery:
我使用它在树中创建隐藏/显示切换 - 这为我提供了左侧所需的按钮,并允许我链接到父级。
I wrapped the link and the text separately -- the :before goes on the container and the link goes inside. This way I can use the :before as a jQuery tigger and the text as a link.
HTML:
CSS:
Jquery:
I'm using this to create a hide/show toggle in a tree -- this gives me the button I need on the left and allows me to link to the parent.