【问题标题】:How to change style properties of an element in JavaScript?如何在 JavaScript 中更改元素的样式属性?
【发布时间】:2022-01-16 05:55:03
【问题描述】:

我正在制作一个小型在线应用程序,它需要用户输入一些选项,并且网站会根据他们的输入更改其背景和类似功能。
但是,我无法通过 JavaScript 代码更改任何文档样式属性。

我错过了什么吗?

let bgType = prompt("Which type of background do you want? Color or image: ");

if (bgType == "color" || bgType == "Color") {
    let colBg = prompt("Enter the color you want");
    document.getElementById("background").style.backgroundColor = colBg; // I've tried putting default colors here for testing, but those didn't work either
} else if (bgType == "image" || bgType == "Image") {
    let imgBg = prompt("Enter the number of the image you want (1-3): ");
    document.getElementById("background").style.backgroundColor = ""; // Unfinished
}

如果这里还需要 HTML 和 CSS 代码 HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <title>Greeting card generator</title>
        <meta name='viewport' content='width=device-width, initial-scale=1'>
        <link rel='stylesheet' type='text/css' media='screen' href='gfx.css'>
    </head>
    <body>
        <script src='script.js'></script>
        <div id="background" class="content">
            <!-- <img src="Temp1.jpg" alt="Greeting card image"> -->
            <h1 id="Title" class="text">
                Sample title text
            </h1>
            <h2 id="Name" class="text">
                Sample name text
            </h2>
            <h3 id="Message" class="text">
                Sample message text
            </h3>
        </div>
    </body>
</html>

CSS:

html, body {
    height: 100%;
    margin: 0;
}

#background {
    width: 100%;
    min-height: 100%;
    background-image: url();
    background-color: white;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

.text {
    font-family: verdana;
    text-align: center;
}

#Title {
    padding-top: 20%;
}

【问题讨论】:

  • 这段代码应该可以工作,除非你的 HTML 有问题,所以请将它添加到问题中。

标签: javascript html css styles element


【解决方案1】:

问题是脚本运行之前 DOM 完全构建并且您要设置样式的元素实际存在。

您需要将&lt;script&gt; 标签移动到&lt;body&gt; 元素的末尾:

 <body>
    <div id="background" class="content">
      <!-- <img src="Temp1.jpg" alt="Greeting card image"> -->
      <h1 id="Title" class="text">Sample title text</h1>
      <h2 id="Name" class="text">Sample name text</h2>
      <h3 id="Message" class="text">Sample message text</h3>
    </div>
    <script src="script.js"></script>
  </body>

【讨论】:

    【解决方案2】:

    只需在 colBg 的开头添加“#”即可。像这样使用

    <script>
        let bgType = prompt("Which type of background do you want? Color or image: ");
    
        if (bgType == "color" || bgType == "Color") {
            let colBg = prompt("Enter the color you want");
            document.getElementById("background").style.backgroundColor = `#${colBg}`; // I've tried putting default colors here for testing, but those didn't work either
        } else if (bgType == "image" || bgType == "Image") {
            let imgBg = prompt("Enter the number of the image you want (1-3): ");
            document.getElementById("background").style.backgroundColor = ""; // Unfinished
        }
    </script>
    

    确保您的输入始终采用 #fff

    之类的颜色值

    【讨论】:

    • 感谢您的意见,但很遗憾,这并没有解决问题。您是否可能更改了代码中的其他内容以使其正常工作?
    • 请查看更新后的答案
    【解决方案3】:

    当您发送颜色代码时,您将添加#。如果发送颜色的名称,则必须发送所有名称。像这样:color:#000color:Black

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 2020-12-30
      • 2010-09-25
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多