javascript - 动态生成的按钮样式类不起作用

标签 javascript css dynamically-generated

我有 40 个动态生成的按钮,它们的类属性为“line”,问题是 line 类的样式没有为 40 个按钮设置样式,因为它是三个不是动态生成的按钮。我可以在 chrome 开发人员中看到“行”应用于所有 40 个按钮。

javascript:

let buttons = [];
const CLS = ["thin", "button"];
let add_btns = document.querySelector('.add-btn');
add_btns.addEventListener("click", function() {
    let i = 0;
    while (i <= 40) {
        let btn = document.createElement("BUTTON");
        buttons.push(document.body.appendChild(btn));
        i++;
        for (let button in buttons) {
            btn.setAttribute('id', "button " + button);
            btn.classList.add(...CLS);
            btn.innerHTML = "Button # " + button;
        }
        btn.addEventListener("click", function() {
            let id = this.id;
            alert("my id is: " + id);
        });
    }
});

css:

html h1,
body h1 {
  margin-top: -5rem;
  text-align: center;
  color: #41403E;
  font-size: 3rem;
}

html section,
body section {
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin-bottom: 3rem;
}

html section button,
body section button {
  align-self: center;
  background: transparent;
  padding: 1rem 1rem;
  margin: 0 1rem;
  transition: all .5s ease;
  color: #41403E;
  font-size: 2rem;
  letter-spacing: 1px;
  outline: none;
  box-shadow: 20px 38px 34px -26px rgba(0, 0, 0, 0.2);
  border-radius: 255px 15px 225px 15px/15px 225px 15px 255px;
}

html section button:hover,
body section button:hover {
  box-shadow: 2px 8px 4px -6px rgba(0, 0, 0, 0.3);
}

html section button.dotted,
body section button.dotted.thick {
  border: dotted 5px #41403E;
}

html section button.thin,
body section button.lined.thin {
  border: solid 2px #41403E;
}

最佳答案

我认为您没有将按钮附加到您的 section 元素。从您的 CSS 中,您想要的样式将仅应用于 section 元素中的按钮。但是您将动态创建的按钮附加到文档 body 中。

尝试为您的 section 元素提供一个 id,引用它,然后将动态创建的按钮附加到它。

像这样的东西应该可以工作:

let buttons = [];
let section = document.getElementById("section");
const CLS = ["thin", "button"];
let add_btns = document.querySelector('.add-btn');
add_btns.addEventListener("click", function() {
  let i = 0;
  while (i <= 40) {
    let btn = document.createElement("BUTTON");
    buttons.push(section.appendChild(btn));
    i++;
    for (let button in buttons) {
      btn.setAttribute('id', "button " + button);
      btn.classList.add(...CLS);
      btn.innerHTML = "Button # " + button;
    }
    btn.addEventListener("click", function() {
      let id = this.id;
      alert("my id is: " + id);
    });
  }
});
html h1,
body h1 {
  margin-top: -5rem;
  text-align: center;
  color: #41403E;
  font-size: 3rem;
}

html section,
body section {
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin-bottom: 3rem;
}

html section button,
body section button {
  align-self: center;
  background: transparent;
  padding: 1rem 1rem;
  margin: 0 1rem;
  transition: all .5s ease;
  color: #41403E;
  font-size: 2rem;
  letter-spacing: 1px;
  outline: none;
  box-shadow: 20px 38px 34px -26px rgba(0, 0, 0, 0.2);
  border-radius: 255px 15px 225px 15px/15px 225px 15px 255px;
}

html section button:hover,
body section button:hover {
  box-shadow: 2px 8px 4px -6px rgba(0, 0, 0, 0.3);
}

html section button.dotted,
body section button.dotted.thick {
  border: dotted 5px #41403E;
}

html section button.thin,
body section button.lined.thin {
  border: solid 2px #41403E;
}
<section id="section">
  <button class="add-btn">
Button
</button>
</section>

关于javascript - 动态生成的按钮样式类不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51323144/

相关文章:

javascript - 覆盖 model.save() 的成功条件

javascript - 我可以从网站上唯一地识别移动设备吗?

CSS 事件不工作

html - 最大宽度 img 无法响应

ios - 如何在 TabBarController 加载其 View 后添加另一个 TabBarItem

javascript - 使用 Nightwatch 测试 Vue 项目时出现 TypeError ERR_UNESCAPED_CHARACTERS

Javascript - 如何使 getTime() 在所有计算机上统一?

CSS 样式 - 如何将这两个 div 框放在相邻的位置

c++ - 十进制生成范围内的随机数,包括负数?

excel - 生成与行值相对应的图纸(存在重复值)