How can I tell if a particular CSS property is inherited with jQuery?

后端 未结 7 787
自闭症患者
自闭症患者 2020-11-29 03:49

This is a very simple question, so I\'ll keep it really brief:

How can I tell if a particular DOM element\'s CSS property is inherited?

Reas

7条回答
  •  时光取名叫无心
    2020-11-29 04:31

    The JS (not jQuery) element.style property returns a CSSStyleDeclaration object thus:

    {parentRule: null, 
    length: 0, 
    cssText: "", 
    alignContent: "", 
    alignItems: ""…
    

    If the style you are interested in has a value then it's declared (or applied by JS) at the element level, else is inherited.
    The information came from https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style

    I was struggling with HTML5 Dnd adding display:list-item at the element level (li) and breaking another hide/show filter functionality; I was able to fix it this way.
    My specific code was:

    // browser bug? it adds display:list-item to the moved elements, this
    // loops clear the explicit element-level "display" style
    var cols = $('#columnNames li');
    for( var icol = 0; icol < cols.length; icol++) {
        var d = cols[icol].style; // the CSSStyleDeclaration 
        d.display = ""; // clear the element-level style
    }
    

提交回复
热议问题