Use javascript to click on a pseudo-element?

前端 未结 5 1488
栀梦
栀梦 2020-12-03 14:37

I\'m wondering how to enable the clicking on a :before pseudo-element (the orange part of the div on the JSfiddle I link to below). I\'ve read that since pseudo

相关标签:
5条回答
  • 2020-12-03 14:39

    My purpose was solved by another workaround which is just adding a child DIV. Wrapping up all child elements inside the parent into this new child DIV:

    My working sample as same as the problem statement: See Fiddle

    HTML:

    <div class="parentDiv">
        :before
        <div class="childDiv">
            <!-- child elements -->
        </div>
    </div>
    

    **Note: Ignore the :before in the HTML, just showing to understand.

    CSS:

    div.parentDiv{position:relative; background-color:#333; padding:0; margin:20px; float:left; }
    div.parentDiv:before { content:""; display:block; padding:5px; background-color:#f60; border:2px solid white; position: absolute; top:-2px; right:-2px; border-bottom-left-radius: 10px; cursor:pointer}
    
    div.childDiv{padding:20px; margin:0}
    

    jQuery:

    jQuery(document).ready(function($){
        $('div.parentDiv').click(function(e){
            if( $(e.target).closest('.childDiv').length==0 ){
                //so clicked on psudo :before element!
                //do your work here ;)
                alert('Psudo :before element is clicked!');
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-03 14:58

    If you know where the circle "should" be, you can use trigonometry to see if the click is within the circle: http://jsfiddle.net/Vv6Eb/19/

    $("div").click(function(e){
        var $me = $(this),
            width = $me.outerWidth(),
            height = $me.outerHeight(),
            top = $me.position().top,
            left = $me.position().left;
    
        var len = Math.sqrt(Math.pow(width - e.offsetX, 2) + Math.pow(e.offsetY, 2));
    
        if (len < 10)
            alert('ding');
    });​
    
    0 讨论(0)
  • 2020-12-03 14:59

    I know you are trying to use :before, but for this situation, can't you just create a new div with a class to use as a hook and append it to the original div?

    Something like this might work:

    var newDiv = $("<div class='orangeCircle'>");
    $(".parentDivToOrangeCircle").append(newDiv);
    

    And the CSS:

    .parentDivToOrangeCircle { position:relative; background-color:#333;
        padding:20px; margin:20px; float:left; 
    }
    
    .orangeCircle {
        padding:5px; background-color:#f60; border:2px solid white; 
        position: absolute; top:-2px; right:-2px; border-bottom-left-radius: 10px; 
    }
    
    0 讨论(0)
  • 2020-12-03 15:04

    A workaround for this would be to dynamically append a <span> to the item and assigning a click method to it. Like this fiddle.

    var item = $('<span />');
    item.click(function() { alert('click'); });
    $('div').append(item);
    

    CSS

    div { position:relative; background-color:#333;
          padding:20px; margin:20px; float:left;
    }
    
    div span { content:""; display:block;
        padding:5px; background-color:#f60; border:2px solid white;
        position: absolute; top:-2px; right:-2px; border-bottom-left-radius: 10px;
    }
    
    0 讨论(0)
  • 2020-12-03 15:04

    Do simply like using jquery

      $(document).on("click", "span", function(e){
             if (e.offsetX > $(this)[0].offsetWidth) {
                   alert('clicked on after');
        } 
        else
        {
        alert('clicked on main span');
        }
            
        })
    div { margin: 20px; }
    span:after { content: 'AFTER'; position: absolute; }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div><span>ELEMENT</span></div>

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