简体   繁体   English

无法获取Firefox元素的HTML元素的高度和宽度

[英]unable to get height and width of html element for firefox

getheightWidth(obj, className)
{
 var o = new Object();
 var span = document.createElement("P");
 document.body.appendChild(span);
 span.className = className;
 span.style.visibility = "hidden";
 o.width = $(span).width();
 o.height = $(span).height();
 return o;
}

I am unable to get height and width of element. 我无法获得元素的高度和宽度。 According to specified in class. 根据类指定。

Your parameter is obj and returning o that's typo: 您的参数是obj并返回错别字的o:

getheightWidth(o, className) //replaced obj to o
{
 var span = document.createElement("P");
 span.className = className;
 span.style.visibility = "hidden";
 o.width = $(span).width();
 o.height = $(span).outerHeight(false);
 return o;
}

try 尝试

SAMPLE HTML 范例HTML

<div class="test1">dd</div>

JS JS

 function getheightWidth(className)
{
 var o = new Object();
 var span = document.createElement("P");
 span.className = className;
 span.style.visibility = "hidden";
 $("div.test1").append(span);   // append into html
 o.width = $(span).width();
 o.height = $(span).outerHeight(false);
 return o;
}
var obj=getheightWidth("test")

console.log(obj);

DEMO 演示

NOTE: You can not get an element's height/width before it is being appended to dom 注意:在将元素附加到dom之前,无法获得元素的高度/宽度

problem was related to window. 问题与窗户有关。 here i was creating element span in current window and try to get on upper window that's why i was enable to get values of variable o. 在这里我正在当前窗口中创建元素跨度,并尝试进入上一个窗口,这就是为什么我能够获取变量o的值。

getheightWidth(obj, className)
{
 var o = new Object();
 var span = document.createElement("P");
 window.top.document.body.appendChild(span);
 span.className = className;
 span.style.visibility = "hidden";
 o.width = $(span).width();
 o.height = $(span).height();
 return o;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM