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

时间:2018-07-13 10:32:06

标签: javascript css dynamically-generated

我有40个动态生成的按钮,它们的类属性均为“ line”,问题是line class的样式没有为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;
}

2 个答案:

答案 0 :(得分:2)

我认为您没有将按钮添加到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>

答案 1 :(得分:0)

从示例中可以看到。选择器在动态生成时起作用。我相信您在CSS中使用嵌套选择器时遇到了问题。

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);
      });
  }
});
.button {
  padding: 10px;
}

.thin {
  padding: 0px 10px;
}
<button class="add-btn button">Add</button>