【问题标题】:How can I create a dynamic background with CSS? [duplicate]如何使用 CSS 创建动态背景? [复制]
【发布时间】:2018-11-16 20:16:23
【问题描述】:

我有一个问题:可以根据一个条件交换背景吗?示例:

function A() {
  if (meuArray.length === 7) {
    i want background green
  } else {
    background blue
  }
}

起初我在巴西做一款名为 Black Jack 或 21 的纸牌游戏,到目前为止,我还没有在包含每张纸牌值的数组中找到可行的背景形式或 CSS。

有人有小费或怎么做吗?谢谢。

【问题讨论】:

  • 是的,你可以。只需获取 dom 的 id 并使用此代码。 document.getElementById('iwantgreen').style.backgroundColor = "green";

标签: javascript css


【解决方案1】:

您可以使用 JavaScript 来实现。假设您要更改具有 id="demo"

的元素的背景

然后你可以这样编码:

if (meuArray.length === 7) {
    document.getElementById("demo").style.backgroundColor = "green";
} 
else {
    document.getElementById("demo").style.backgroundColor = "blue";
}

如果您想选择一个班级,请使用:

    document.getElementsByClassName("demo")

【讨论】:

    【解决方案2】:

    使用document.getElementById获取dom并设置backgroundColor

    function A() {
          if (meuArray.length === 7) {
            document.getElementById('domId').style.backgroundColor = 'green'
          } else {
            document.getElementById('domId').style.backgroundColor = 'blue'
          }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 1970-01-01
      • 2020-02-11
      • 2021-05-29
      • 2020-10-06
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多