How do I detect the inherited background-color of an element using jQuery/JS?

后端 未结 6 921
礼貌的吻别
礼貌的吻别 2020-11-30 11:40

Using jQuery (or just JavaScript), how do I detect the inherited background-color of an element?

For example:

6条回答
  •  忘掉有多难
    2020-11-30 11:58

    Hacky-Recursive answer

    jQuery.fn.InheritedBackgroundColor = function(){    
       jQuery(this).parents().each( function(){
          var bc = jQuery(this).css("background-color");
          if( bc == "transparent" ){
             return jQuery(this).InheritedBackgroundColor();
          }
          else{
             return bc;
          }      
       });
    }
    
    $(document).ready(function(){
       if( $("#target").InheritedBackgroundColor() == rbga(255,0,0,0) ){
          alert("Win!");
       }
       else{
          alert("FAIL!");
       }
    
    });
    

提交回复
热议问题