只是一个想法......不要讨厌。我知道它并没有涵盖所有可能发生的情况,但是这样的事情可以检查每个父项是否有所需的 css 规则(显示:无;在这种情况下)。我从这里得到了这个想法。
唯一的问题是当网站变得更复杂时......它变得很慢。
但这是一个起点。
//showing as 100%
alert($('#test2').width());
//use window.load to ensure .width() isnt doing anything funny
$(window).load(function(){
//checks to see if any of its parents have display:none; we can have multiple checks here if required
if ($( "#test2" ).parents().css('display') == 'none') {
//iterate through each parent of the selected item
$( "#test2" ).parents().each(function () {
//only change the display value if the element has a display:none;
if ($(this).css('display') == 'none') {
$(this).css('display', 'block');
alert($('#test2').width());//or whatever we want to do with this number
//reset values here
$(this).css('display', 'none');
}
});
}
//showing as 100%
alert($('#test2').width());
});