CSS: Prevent parent element getting :active pseudoclass when child element is clicked

后端 未结 12 930
暖寄归人
暖寄归人 2021-01-07 16:19

JSFiddle

When you click the button, you see that :active pseudoclass is triggered for the parent div. Is ther

12条回答
  •  梦谈多话
    2021-01-07 17:00

    here's a jquery solution instead of using the css pseudo class :active

    $(document).ready(function() {
        $('button').mousedown(function(e){
            e.stopPropagation();
            console.log('i got clicked');
        });
    
        $('div').mousedown(function(e){
            $('div').css('background', 'red')
        }).mouseup(function(e){
            $('div').css('background', '#eee')
        });
        $(document).mouseup(function(e){
            $('div').css('background', '#eee')
        });
    });
    div {
      width: 200px;
      height: 200px;
      background-color: #eee;
      border: 1px solid black;
    }
    
    

提交回复
热议问题