Possible to make a:after/before pseudo elements clickable as part of the link?

后端 未结 8 1701
不知归路
不知归路 2020-12-16 09:42

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

相关标签:
8条回答
  • 2020-12-16 09:59

    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, setting opacity to anything other than 1) works.

    0 讨论(0)
  • 2020-12-16 10:02

    If you have a link on Wrapper then you can make pseudo-elements clickable by setting pointer-events to none.

    a:after {
     pointer-events: none;
    }
    
    0 讨论(0)
  • 2020-12-16 10:04

    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:

    <li class="before-here"><a href="#">My Link Text</a></li> 
    

    CSS:

    li.before-here:before{ //some css like an image }
    

    Jquery:

    $("li.before-here").click(function(){ //do something});
    

    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.

    0 讨论(0)
  • 2020-12-16 10:07

    To avoid modifying the document tree, you could use a JavaScript click handler for a:after.

    Edit: This doesn't work, because pseudo elements aren't added to the DOM.

    0 讨论(0)
  • 2020-12-16 10:08

    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.

    0 讨论(0)
  • 2020-12-16 10:21

    I've had the same problem and apparently if I set

    a { vertical-align: middle; }
    

    (thus on the link itself, not the pseudo element), it works.

    0 讨论(0)
提交回复
热议问题