最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - how to find the size of a div in javascript? - Stack Overflow

programmeradmin3浏览0评论

i am trying to figure this out:

i have some html and php:

<div class="main" style="height: 800px">
    <div class="head" style="height: 300px"</div>
    <div class="container">
        <?php
        for ($i=0; $i<=$size;$i++){
            echo "<div style="/"height: 20px;/">line $i</div>";
        }
        ?>
     </div>
</div>

now, what i am trying to do is to find the height of the .container div and apply it to $size

if the .container height is 100 then i will do something like: $size = 100 / 20 or something similar.

The issue is that even if i find the div height it is still on the client side and i might have to do some ajax.

Another problem is that if there is no content in the .container usually there is no height.

in this case i could force the size of .container to be 800 - 300 but i don't allays know other sizes from different block elements on the page.

i hope i am not to confuse with this question.

any ideas?

Thanks

i am trying to figure this out:

i have some html and php:

<div class="main" style="height: 800px">
    <div class="head" style="height: 300px"</div>
    <div class="container">
        <?php
        for ($i=0; $i<=$size;$i++){
            echo "<div style="/"height: 20px;/">line $i</div>";
        }
        ?>
     </div>
</div>

now, what i am trying to do is to find the height of the .container div and apply it to $size

if the .container height is 100 then i will do something like: $size = 100 / 20 or something similar.

The issue is that even if i find the div height it is still on the client side and i might have to do some ajax.

Another problem is that if there is no content in the .container usually there is no height.

in this case i could force the size of .container to be 800 - 300 but i don't allays know other sizes from different block elements on the page.

i hope i am not to confuse with this question.

any ideas?

Thanks

Share Improve this question asked Jan 10, 2012 at 18:50 PatrioticcowPatrioticcow 27.1k76 gold badges221 silver badges340 bronze badges 1
  • 2 You can't make operation with php on client side. It muts be parsed on the server side. Javascript will do the trick. – Michael Sazonov Commented Jan 10, 2012 at 18:53
Add a ment  | 

1 Answer 1

Reset to default 7

Description

You can use jQuery's height() and width() method to get the size.

Check out the sample and jsFiddle Demonstration

Sample

html

<div class="container">
    MyContent<br><br>
</div>

jQuery

alert($(".container").height())
alert($(".container").width())

More Information

  • jsFiddle Demonstration
  • jQuery.height()
  • jQuery.width()
发布评论

评论列表(0)

  1. 暂无评论