Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript操作div样式? div { 宽度:5em; 身高:5公分; 背景颜色:黄色; } window.onload=函数(){ var x; x=document.getElementByID(“div”); x、 style.width='9em'; x、 style.backgroundColor=“绿色”; };_Javascript_Html_Css - Fatal编程技术网

如何使用javascript操作div样式? div { 宽度:5em; 身高:5公分; 背景颜色:黄色; } window.onload=函数(){ var x; x=document.getElementByID(“div”); x、 style.width='9em'; x、 style.backgroundColor=“绿色”; };

如何使用javascript操作div样式? div { 宽度:5em; 身高:5公分; 背景颜色:黄色; } window.onload=函数(){ var x; x=document.getElementByID(“div”); x、 style.width='9em'; x、 style.backgroundColor=“绿色”; };,javascript,html,css,Javascript,Html,Css,我做错了什么?我想用javascript更改div属性,如宽度或背景色,但它不起作用。将getElementByID更改为getElementByID div { 宽度:5em; 身高:5公分; 背景颜色:黄色; } window.onload=函数(){ var x; x=document.getElementById(“div”); x、 style.width='9em'; x、 style.backgroundColor=“绿色”; }; 将getElementByID更改为getE

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

getElementByID
更改为
getElementByID


div
{
宽度:5em;
身高:5公分;
背景颜色:黄色;
}
window.onload=函数(){
var x;
x=document.getElementById(“div”);
x、 style.width='9em';
x、 style.backgroundColor=“绿色”;
};

getElementByID
更改为
getElementByID


div
{
宽度:5em;
身高:5公分;
背景颜色:黄色;
}
window.onload=函数(){
var x;
x=document.getElementById(“div”);
x、 style.width='9em';
x、 style.backgroundColor=“绿色”;
};

您使用的是
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>

您正在使用
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语法错误!这是最新消息


/*div{
宽度:5em;
身高:5公分;
背景颜色:黄色;
} */
var x=document.getElementById(“div”);
x、 style.width=“9em”;
x、 style.height=“9em”;
x、 style.display=“block”;
x、 style.backgroundColor=“绿色”;

JavaScript语法错误!这是最新消息


/*div{
宽度:5em;
身高:5公分;
背景颜色:黄色;
} */
var x=document.getElementById(“div”);
x、 style.width=“9em”;
x、 style.height=“9em”;
x、 style.display=“block”;
x、 style.backgroundColor=“绿色”;

将getElementByID更改为getElementByID

window.onload = function() {
  var x;
  x = document.getElementById("div");
  x.style.width = '9em';
  x.style.backgroundColor = "green";
};

如果您想通过标记名进行搜索,可以直接使用getElementsByTagName(“div”)而不是分配值div的id。但请记住,它将返回所有div元素;i、 e.元素的集合。

将getElementByID更改为getElementByID

window.onload = function() {
  var x;
  x = document.getElementById("div");
  x.style.width = '9em';
  x.style.backgroundColor = "green";
};

如果您想通过标记名进行搜索,可以直接使用getElementsByTagName(“div”)而不是分配值div的id。但请记住,它将返回所有div元素;i、 e.元素的集合。

getElementByID
应该是
getElementByID
最好查看浏览器的JavaScript控制台,即对于Google Chrome,按CTRL-SHIFT-i并选择console选项卡。在这里,您将看到一条错误消息,如“Uncaught TypeError:document.getElementByID不是一个函数”,它指向解决方案,正如Suresh已经提到的。
getElementByID
应该是
getElementByID
在浏览器的JavaScript控制台中查看总是一个好主意,例如,对于Google Chrome,按CTRL-SHIFT-i并选择console选项卡。在这里,您将看到一条错误消息,如“UncaughtTypeError:document.getElementByID不是一个函数”,它指向解决方案,正如Suresh已经提到的。最好解释一下您所做的更改,与其期望人们将您的代码与原始代码进行比较,以发现(在本例中)单个字符的更改总是更好地解释您所做的更改,还不如期望人们将您的代码与原始代码进行比较,以发现(在本例中)单个字符的更改我道歉。更正了,我道歉。更正了。