Only detect click event on pseudo-element

前端 未结 10 1866
长情又很酷
长情又很酷 2020-11-22 08:00

My code is:

p {
    position: relative;
    background-color: blue;
}

p:before {
    content: \'\';
    position: absolute;
    left:100%;
    width: 10px;
         


        
相关标签:
10条回答
  • 2020-11-22 08:50

    Add condition in Click event to restrict the clickable area .

        $('#thing').click(function(e) {
           if (e.clientX > $(this).offset().left + 90 &&
                 e.clientY < $(this).offset().top + 10) {
                     // action when clicking on after-element
                     // your code here
           }
         });
    

    DEMO

    0 讨论(0)
  • 2020-11-22 08:50

    No,but you can do like this

    In html file add this section

    <div class="arrow">
    </div>
    

    In css you can do like this

    p div.arrow {
        content: '';
        position: absolute;
        left:100%;
        width: 10px;
        height: 100%;
        background-color: red;
    } 
    

    Hope it will help you

    0 讨论(0)
  • 2020-11-22 08:52

    On modern browsers you can try with the pointer-events css property (but it leads to the impossibility to detect mouse events on the parent node):

    p {
        position: relative;
        background-color: blue;
        color:#ffffff;
        padding:0px 10px;
        pointer-events:none;
    }
    p::before {
        content: attr(data-before);
        margin-left:-10px;
        margin-right:10px;
        position: relative;
        background-color: red;
        padding:0px 10px;
        pointer-events:auto;
    }
    

    When the event target is your "p" element, you know it is your "p:before".

    If you still need to detect mouse events on the main p, you may consider the possibility to modify your HTML structure. You can add a span tag and the following style:

    p span {
        background:#393;
        padding:0px 10px;
        pointer-events:auto;
    }
    

    The event targets are now both the "span" and the "p:before" elements.

    Example without jquery: http://jsfiddle.net/2nsptvcu/

    Example with jquery: http://jsfiddle.net/0vygmnnb/

    Here is the list of browsers supporting pointer-events: http://caniuse.com/#feat=pointer-events

    0 讨论(0)
  • 2020-11-22 08:53

    This is edited answer by Fasoeu with latest CSS3 and JS ES6

    Edited demo without using JQuery.

    Shortest example of code:

    <p><span>Some text</span></p>
    
    p {
        position: relative;
        pointer-events: none;
    }
    p::before {
        content: "";
        position: absolute;
        pointer-events: auto;
    }
    p span {
        display: contents;
        pointer-events: auto;
    }
    
    const all_p = Array.from(document.querySelectorAll('p'));
    
    for (let p of all_p) {
        p.addEventListener("click", listener, false);
    };
    

    Explanation:

    pointer-events control detection of events, removing receiving events from target, but keep receiving from pseudo-elements make possible to click on ::before and ::after and you will always know what you are clicking on pseudo-element, however if you still need to click, you put all content in nested element (span in example), but because we don't want to apply any additional styles, display: contents; become very handy solution and it supported by most browsers. pointer-events: none; as already mentioned in original post also widely supported.

    The JavaScript part also used widely supported Array.from and for...of, however they are not necessary to use in code.

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