javascript style.width无法在firefox中使用过渡doctype

时间:2008-12-08 15:27:53

标签: javascript css doctype transitional

我有一个脚本,可以在页面上弹出一个小DIV。它在IE中都可以正常工作,如果我删除了DOCTYPE,那么在FF中,但是当DOCTYPE是XHTML / Transitional时,在Firefox中,宽度不会改变。

this.container.style.visibility = "visible";
alert("this.container.style.width before = " + this.container.style.width)
this.container.style.width = this.width;
alert("this.container.style.width after = " + this.container.style.width); 
this.container.style.height = this.height;

在IE中,在没有DOCTYPE的FF中,第一个警报显示0,第二个警告显示320(这是代码中其他地方设置的宽度)

在FF中,使用DOCTYPE到XHTML / Transitional,两个警报都显示为0.任何想法在这里发生了什么?我想我可能需要在Transitional中明确设置DIV的位置,但我不确定。

2 个答案:

答案 0 :(得分:16)

您是否尝试过设置:

this.container.style.visibility = "visible";
alert("this.container.style.width before = " + this.container.style.width);
this.container.style.width = this.width + 'px';
alert("this.container.style.width after = " + this.container.style.width);
this.container.style.height = this.height + 'px';

//Note the 'px' above

我发现尝试设置数字的宽度/高度,没有单位会导致问题。

答案 1 :(得分:0)

您可以使用:

document.getElementById("td").style.visibility="hidden";
document.getElementById("td").style.display="none";

而不是width属性。

有效!