2017-05-07 46 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()); 
    }); 
}