jquery .ready and height of element

前端 未结 1 1498
天命终不由人
天命终不由人 2020-12-03 15:05

Might seem like a trivial question but I\'ve run into an issue while using jQuery. When I try and get the height for an element within a .ready I am always given zero.

相关标签:
1条回答
  • 2020-12-03 15:32

    The document.ready event signals that the HTML DOM is ready for accessing via Javascript, but that doesn't mean that the elements have already rendered.

    In fact, that's the whole shebang behind ready: it's a means for you to start manipulating your document's HTML DOM without having to wait for the page to finish loading. It's safe to assume that at document.ready, your elements are not yet displayed on the page.

    Now, that comes with a caveat: if the elements haven't rendered yet, how can the browser / Javascript know what its resolving height is? .height() may give zero at document.ready because of this. It's probably best to wait until load instead of ready when it comes to pulling box dimensions from the layout.

    0 讨论(0)
提交回复
热议问题