javascript - CSS: Is it possible to make the ::after selector behave like a button? - Stack Overflow

Look at the code of below, as you can see there is a close icon floated to the right of a span element.

Look at the code of below, as you can see there is a close icon floated to the right of a span element.

span {
    width: 100%;
    display: inline-block;
}

span:after {
    content: "\2715";
    float: right;
    position: absolute;
}

span:hover:after {
    cursor: pointer;
}
<span>Content</span>

Look at the code of below, as you can see there is a close icon floated to the right of a span element.

span {
    width: 100%;
    display: inline-block;
}

span:after {
    content: "\2715";
    float: right;
    position: absolute;
}

span:hover:after {
    cursor: pointer;
}
<span>Content</span>

I want the :after to behave like a button. As you could see, it makes the cursor a pointer on hover. How can I make it to behave like a button? For example, how can I add an onclick function to it?

Share Improve this question edited Oct 7, 2019 at 1:58 Dacre Denny 30.4k5 gold badges51 silver badges66 bronze badges asked Oct 6, 2019 at 23:11 Martín NievaMartín Nieva 5041 gold badge5 silver badges15 bronze badges 2
  • 4 Pseudo-elements aren’t present in the DOM, and can’t be targeted/selected with JavaScript; therefore I don’t believe they can be made to act like a <button> or any other interactive element (unless the element to which the pseudo is ‘attached’ also behaves the same way. – David Thomas Commented Oct 6, 2019 at 23:19
  • Since the pseudo element is a child of the <span>, you can attach event handlers to that – Phil Commented Oct 6, 2019 at 23:21
Add a ment  | 

1 Answer 1

Reset to default 10

Generally speaking, the pseudo element will inherit the events and event behavior assigned to the non-pseudo element that "owns" it.

So for instance, adding a click event listener to the above <span> will cause any pseudo elements of that span elemenet to inherit the same click event behavior.

If you wanted to achieve independence between the pseudo element and the "owner" element, in terms of click behavior (ie click behavior for the "pseudo" element only) you could use the pointer-events CSS property as shown below:

const span = document.querySelector("span");

span.addEventListener("click", () => alert("hey!"));
span {
    display: inline-block;
    position: relative;
    background:red;
    /* Blocks the click event from firing when span is clicked */
    pointer-events:none;
}

span:after {
    content: "'Pesudo button' - click me!";
    position: absolute;
    margin-left:1rem;
    background:green;
    width:15rem;
    text-align:center;
    color:white;
    /* Allows the click event to fire when the pesudo element is clicked */
    pointer-events:all;
}

span:hover:after {
    cursor: pointer;
}
<span>I own the pesudo element</span>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745655051a4638493.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信