javascript - 如何使用 javascript 操作 div 样式?

标签 javascript html css

<!DOCTYPE html>
<html>
    <head>
        <title>
        </title>
        <style>
        div
        {
            width:5em;
            height:5em;
            background-color:yellow;
        }
        </style>
    </head>
    <body>
        <div id="div">
        </div>
        <script>
        window.onload = function() {
        var x;
        x = document.getElementByID("div");
        x.style.width = '9em' ;
        x.style.backgroundColor = "green";
    };

        </script>
    </body>
</html>

我做错了什么?我想使用 javascript 更改宽度或背景颜色等 div 属性,但它不起作用。

最佳答案

getElementByID 更改为 getElementById

<!DOCTYPE html>
<html>
    <head>
        <title>
        </title>
        <style>
        div
        {
            width:5em;
            height:5em;
            background-color:yellow;
        }
        </style>
    </head>
    <body>
        <div id="div">
        </div>
        <script>
        window.onload = function() {
        var x;
        x = document.getElementById("div");
        x.style.width = '9em' ;
        x.style.backgroundColor = "green";
    };

        </script>
    </body>
</html>

关于javascript - 如何使用 javascript 操作 div 样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44475642/

相关文章:

html - 如何将图像移动到 li manu 的右侧?

html - 使用 React 向 HTML <body> 标签添加一个类?

javascript - 如何进行模态转换?

javascript - 多年来的下拉列表

javascript - Node.js setInterval 和 nextTick 第二次调用不准确

html - 如何设置具有两个区域的 BorderContainer?

jquery - 使DIV标签均匀

html - CSS 问题 - 将容器内的 float div 居中

javascript - 如何在 HTML 和 JavaScript 中检测用户是否选择了整个文档?

javascript - 使用 SAPUI5 中响应表的实时数据生成 PDF 文件