0

We all know that jQuery doesn't calculate the width of hidden elements. Right? Has this changed in jQuery 1.10?

In this jsFiddle, the width of the hidden LI <li style="display:none;">a hidden li</li> is calculated. Why is this?

And if a recent update changed the behavior, how can I make sure that width ISN'T calculate? I tried:

$('#theList li').each(function() {
    totalWidth += $(this).is(':visible').width();
});

But that didn't work either - it still returned the element and its width.

4

1 回答 1

1

您可能需要先检查元素是否可见,然后执行您的功能。

var totalWidth = 0;

$('#theList li').each(function() {
    if ($(this).is(":visible")){ // CHECK FIRST IF VISIBLE
    var $this = $(this);
    totalWidth += $this.width();

    $('#theListItems').append($this.text() + " ("  +$this.width() + " width)<br>");
    $('#totalWidth').html(totalWidth);
    }
});

jsfiddle:http: //jsfiddle.net/javascript/LQZB2/

于 2013-07-15T21:16:58.393 回答