2017-05-07 47 views
0

我想要在添加最后一个元素之后获取div高度,而不使用css溢出。如何在添加jquery后添加div高度而不使用css溢出

HTML代码:

<div id="wall"></div> 

Javacript代码:

var walldown = setInterval(walldownfn, 10000); 

    function walldownfn() { 

     $.post("/php/post.php", function(data, status){ 
      $("#wall").append(data+"<br>"); 

     }); 
     var awrap = document.getElementById("wall"); 
     alert(awrap); 
     } 

报警输出:

[object HTMLDivElement] or 0 height 

Ajax响应数据是HTML代码。

例子:

<div class="card" style="width: 20rem;"> 
    <img class="card-img-top" src="..." alt="Card image cap"> 
    <div class="card-block"> 
    <h4 class="card-title">Card title</h4> 
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> 
    <a href="#" class="btn btn-primary">Go somewhere</a> 
    </div> 
</div> 
+0

你试过'$(“#wall”)。height()'? – HtmHell

+0

是的,但是也总是得到0高度 – ind

+0

你需要在函数内部追加内容后调用它。 – HtmHell

回答

0

使用$("#wall").height()你让每一个延时股利HIGHT见代码波纹管(控制台,而不是警告)

var walldown = setInterval(walldownfn, 1500); 
 

 
var returned_data = '<div class="card" style="width: 20rem;"><img class="card-img-top" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSQythd0ly14qbZd7YEJ9WreTX4-dBQw7vfGS33ZXbTQRJjE1AHITKZE5g" alt="Card image cap"><div class="card-block"><h4 class="card-title">Card title</h4><p class="card-text">Some quick example text to build on the card title and make up the bulk of the card\'s content.</p><a href="#" class="btn btn-primary">Go somewhere</a></div></div>'; 
 
function walldownfn() { 
 

 
    $.post("https://httpbin.org/post", {text:returned_data},function(data, status){ 
 
     $("#wall").append(data.form.text); 
 
     console.log("height : "+$("#wall").height()); 
 
    }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wall"></div>

+0

使用ajax时这不起作用。我的主要内容是使用阿贾克斯抱歉,因为它不知情。使用时只能得到新的线高。 – ind

+0

@ind这不是问题中提到的问题,请重新表达您的问题,而您的意思是使用ajax?你用阿贾克斯grabe信息,然后你在该div中呈现它? –

+0

年你是正确的后,我想要div高度的数据呈现到div后的ajax。我总是得到0而不使用br。如果我使用br,它只能获得断线高度约12px不包含高度 – ind

0

试试这个:

var walldown = setInterval(walldownfn, 10000); 

function walldownfn() { 
    $.post("/php/post.php", function(data, status){ 
     $("#wall").append(data+"<br>"); 
     alert($("#wall").height()); 
    }); 
}