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;
}
我无法获得元素的高度和宽度。根据课堂上的规定。
答案 0 :(得分:2)
您的参数是obj并返回错误的信息:
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;
}
答案 1 :(得分:1)
试
<div class="test1">dd</div>
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);
注意:在将元素附加到dom之前,您无法获得元素的高度/宽度
答案 2 :(得分:0)
问题与窗口有关。在这里,我在当前窗口创建元素跨度,并试图进入上窗口,这就是为什么我可以获取变量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;
}