在标准模式下设置元素宽度或高度 [英] Set element width or height in Standards Mode

查看:255
本文介绍了在标准模式下设置元素宽度或高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在标准模式下在JavaScript中设置HTML元素的宽度或高度(例如< div> )?

Is it possible to set width or height of HTML element (ex. <div>) in JavaScript in Standards Mode?

请注意以下代码:

<html>
<script language="javascript" type="text/javascript">
    function changeWidth(){
        var e1 = document.getElementById("e1");
        e1.style.width = 400;
    } 
</script>
<body>
    <input type="button" value="change width" onclick="changeWidth()"/>
    <div id="e1" style="width:20px;height:20px; background-color:#096"></div>
</body>
</html>

当用户按更改宽度按钮时,< div> 的宽度应该更改。

When user presses the change width button, the <div>'s width should change.

当doctype声明确定Quirks模式时,它可以正常工作。在标准模式下,我无法以这种方式更改元素的大小

It works fine when doctype declaration determines Quirks Mode. In Standards Mode I'm unable to change the element's size this way

是否可以在标准模式下操作元素的大小?如何绕过这种失效?

Is it possible to manipulate the size of an element in Standards Mode? How to bypass this disfunctionality?

推荐答案

尝试声明宽度单位:

e1.style.width = "400px"; // width in PIXELS

这篇关于在标准模式下设置元素宽度或高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆