Difference between $(window).width() vs $(document).width()

后端 未结 6 1018
灰色年华
灰色年华 2020-12-08 03:52

What is the major difference between $(window).width() vs $(document).width() in jQuery? Whether window denotes the browser and document represents

6条回答
  •  鱼传尺愫
    2020-12-08 04:18

    Another important difference.

    $(window).width(); is available before the document loads / is ready

    $(document).width(); is only available after the document had loaded

    So for the second, you need

    $(document).ready(function() {
       var w = $(document).width();
    });
    

提交回复
热议问题