Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty tag elements and :visible?

I'm using the latest jquery version.(1.9.1)

I have an empty span element :

<!DOCTYPE html>
 <html>

    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <meta charset=utf-8 />
        <title>JS Bin</title>
    </head>

    <body> <span class="c"></span>

    </body>
    <script type='text/javascript'>
        alert($(".c:visible").length);
    </script>

</html>
  • Chrome ( latest ver ) alerts 0
  • FF (latest ver) alerts 1
  • Ie (8) alert 0

Why doesn't it alert1 at chrome ? the element is not hidden.

I can work around this by using filter and spit out all the !display:none but wanted to know if i'm doing anything wrong

http://jsbin.com/urihov/2/edit

like image 820
Royi Namir Avatar asked Jul 31 '26 07:07

Royi Namir


1 Answers

@inser is right a span is an inline element and only gains layout when it has content thus gaining the width and height of its content - in this case the width and height are 0 so not visible to the, if you add this css rule

.c {
  display: block;
}

you will see that the alert says 1 now since you have forced it to be a block element now thus inheriting the width of its parent and gaining layout.

hope it helps,

Cheers

like image 70
Ady Ngom Avatar answered Aug 01 '26 19:08

Ady Ngom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!