Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找div height始终返回零jquery_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 查找div height始终返回零jquery

Javascript 查找div height始终返回零jquery,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我使用jquery查找div的高度,但它总是返回零。我可以在chrome的inspect元素css窗口中清楚地看到div的高度 <div class="findheight">some text here</div>//original height is 21px 所以当我调用height方法时,我认为div可能并没有文本,我喜欢下面的方法 if($('.findheight').text().length > 6){ alert($('.fin

我使用jquery查找div的高度,但它总是返回零。我可以在chrome的inspect元素css窗口中清楚地看到div的高度

<div class="findheight">some text here</div>//original height is 21px
所以当我调用height方法时,我认为div可能并没有文本,我喜欢下面的方法

  if($('.findheight').text().length > 6){
      alert($('.findheight').height());//This also returns zero so weird 
 }
我还尝试设置
windows.timeout
call,但这对任何延迟时间都没有帮助


有人能帮我解释为什么我总是得到零吗?

您是否将jquery封装在dom就绪时执行的函数中

$(document).ready(function(){
  console.log($('.findheight').height());
});
它适用于我,否则您的错误可能是特定于浏览器的

这里有一个JSFIDE正在运行。检查一下你的控制台。

如果出于某种原因,您无法访问documentready上的元素,例如在Meteor、Ember、React或Angular等动态模板化框架中工作,那么每个对象都有一个钩子来注册您的函数

如果您试图在转换之间访问某个元素,则可以设置一个间隔

var interval;
var timeout = setTimeout(function(){
  clearInterval(interval)
}, 10000); // destroy interval after 10 seconds to prevent memory leak
interval = setInterval(function(){
  if($('.findheight').length > 0){ 
    // execute your code here because you know this element is in the dom
    clearInterval(interval);
    clearTimeout(timeout);
    // clear both timeout and interval
  }
}, 100); // loop every 100 milliseconds to "poll" for the node in the dom

如果动态加载div的内容,则需要使用window.load事件获取正确的高度,如下所述:

将代码复制到,高度为18

<div class="findheight">some text here</div>//original height is 21px
alert($('.findheight').height());
此处的一些文本//原始高度为21px
警报($('.findheight').height());
尝试执行以下操作:

$('div.findheight').height(); 
.height()
完全采用CSS的height属性

如果是动态内容,则必须在相应的事件中处理

例如:

  • 如果您正在处理页面加载上的内容,如@renakre所说,请使用
    $(window.load()
    $(document.load()
    触发调用

  • 如果它是从您编码的其他进程生成的内容,则必须绑定相应的事件。。让我们假设这是一次点击:

  • $(文档).load(函数(){
    //显示页面加载时的高度
    log($('.findheight').height());
    $(“#另一个元素ID”)。单击(函数(){
    //…填充div dynamicali的代码
    //显示高度
    log($('.findheight').height());
    });
    $('.other element class')。在('change',function()上{
    //…填充div dynamicali的代码
    //显示高度
    log($('.findheight').height());
    });
    
    });当没有为该元素设置显式的高度/宽度,或者当内部/外部元素的CSS将高度/宽度设置为0时,就会发生这种情况


    要获得这样的高度/宽度,您必须显式设置高度/宽度。

    您是否尝试使用outerHeight()而不是height()?控制台中是否存在任何错误?您是否有多个具有类
    findheight
    的div?包括Jquery吗?您的问题中没有足够的信息,因为代码是功能性的;显示?元素是否不可见(
    display:none
    )?不可见元素没有高度。你能为findheight共享css吗?这可能是一个有趣的例子。但不确定在何处调用此加载函数,因为该div是容器的一部分。如果容器未仅加载,我将在div中填充文本。@rakeeee将其放在
    结束标记之前
    <div class="findheight">some text here</div>//original height is 21px
    alert($('.findheight').height());
    
    $('div.findheight').height();