Find the height of dynamically created div
Neither of the properties clientHeight and offsetHeight has worked out for me while I try to determine the height o开发者_如何学编程f the div created dynamically. Could someone suggest alternative please? Thanks a ton in advance.
Ashriya,
if you are able to use jquery, (as an alternative) then you can easily find (and set) the height of any div (in your domain) as such:
// get the height
var myDivHeight = $('#myDiv').css('height');
// set the height
$('#myDiv').css('height', myDivHeight + someothernumber);
hope this helps a 'little'...
[edit] - forgot to use the # id placeholders in my original response. have corrected above!! - argh :)
I'm not a fan of JQuery. I use the "native" mydiv.offsetHeight - works with IE and FF.
I'm not sure, that I completely understand your problem. However, a following approach may be helpful.
<html>
<body onload="documentReady();">
<script type="text/javascript">
function documentReady() {
var div = document.createElement("div");
div.innerText = "I'm a div, excluded from DOM!";
var helper = div.cloneNode(true);
helper.style.position = "relative";
helper.style.top = "-100000px";
helper.style.left = "-100000px";
document.body.appendChild(helper);
var height = helper.offsetHeight;
var width = helper.offsetWidth;
document.body.removeChild(helper);
alert([height, width]);
}
</script>
</body>
</html>
Use jQuery's outerHeight()
.
精彩评论