2016-12-06 54 views
0

我需要检测,如果我的父母有应用这些CSS样式:如何检查css样式的父元素?

display: flex; 
flex-direction: column; 

我发现a jQuery example但(一)我不使用jQuery,和(b)我不明白的完整的答案。

+0

'parent.style.display ==“flex”;'父母代表你的父母。 – WasteD

+0

它不会工作,因为它只读取由'style'属性设置的css – pwolaq

+0

尝试使用'filter()'http://api.jquery.com/filter/ – prasanth

回答

3

可以使用window.getComputedStyle(element)和默认值测试您的属性,例如:

var element = document.getElementById("example"); 
var styles = window.getComputedStyle(element); 

if(styles.getPropertyValue("display") === "flex"){ 
    // element style display is flex 
} 
+0

试试这个OP。这是对的。但是pwolaq,如果你不介意的话。给OP例子启发OP。 (+1) –

+0

感谢提示,更新 – pwolaq

1

你可以找到在JQuery中使用的过滤器。

$("#Myelement").parents() 
    .filter(function() { 
     return $(this).css("myCssItem") == "myCssValue"; 
    }).first(); 
+0

这是非常低效的解决方案 – pwolaq