javascript - onclick() 函数不运行代码

标签 javascript html onclick

我尝试了多种方法来执行此操作,但 onclick 函数没有响应。我用 Chrome 。因为我是初学者,所以请您回答得清楚一点吗?

<html>
  <head>
    <style>
      .a{
        display:inline;
        background-color: blue;
        color: blue;
        width: 100px;
        float: left;
      }
      .b{

        display:inline;
        background-color: red;
        color: red;
        width: 100px;
        float: left;
      }

      h1:hover{
        background-color: white!important;
        color: white!important;
      }
      .a::selection{
        display:inline;
        background-color: blue;
        color: blue;
        width: 100px;
        float: left;
      }
      .b::selection{
        display:inline;
        background-color: red;
        color: red;
        width: 100px;
        float: left;
      }
    </style>

  </head>
  <body>
    <div id="div" class="div">
    </div>
    <script>
      for(var i = 1;i<10;i){
        var a = document.createElement('h1')
        document.getElementById('div').appendChild(a)
        a.innerHTML=("HI")
        a.id="id"
        document.getElementById("id").setAttribute('class', 'a');
        a.id=null
        a.href=""
        i=i+1


        var b = document.createElement('h1')
        document.getElementById('div').appendChild(b)
        b.innerHTML=("BYE")
        b.id="id"
        document.getElementById("id").setAttribute('class', 'b');
        b.id=null
        b.href=""
        i=i+1
        }

        for(i=1;i<10000;i){
          setTimeout(function(){
            document.getElementsByClassName('a')[0].setAttribute("style","background-color:red;color:red")
            Array.prototype.forEach.call(document.getElementsByClassName('a'),
                             item => item.setAttribute("style","color: blue;background-color:blue"));
          },(2*i-0)*1000)
          setTimeout(function(){
            document.getElementsByClassName('a')[0].setAttribute("style","background-color:blue;color:blue")
            Array.prototype.forEach.call(document.getElementsByClassName('a'),
                             item => item.setAttribute("style","color: red;background-color:red"));
          },(2*i-1)*1000)
          setTimeout(function(){
            document.getElementsByClassName('b')[0].setAttribute("style","background-color:blue;color:blue")
            Array.prototype.forEach.call(document.getElementsByClassName('b'),
                             item => item.setAttribute("style","color: blue;background-color:blue"));
          },(2*i-1)*1000)
          setTimeout(function(){
            document.getElementsByClassName('b')[0].setAttribute("style","background-color:red;color:red")
            Array.prototype.forEach.call(document.getElementsByClassName('b'),
                             item => item.setAttribute("style","color: red;background-color:red"));
          },(2*i-0)*1000)
          i=i+1
        }
        var color = document.querySelectorAll('h1')
        color.onclick = function(){
          alert('debug')
          if (this.style.backgroundColor == 'white') {
            this.style.backgroundColor = '#000001'
            this.style.color = '#000001'
          }
          
          if (this.style.backgroundColor == '#000001') {
            this.style.backgroundColor = 'white'
            this.style.color = 'white'
          }

        }
    </script>

  </body>
</html>

其他部分有效,但 onclick 部分无效。你能帮忙吗?它在 chrome 中没有显示任何错误。上面的代码片段实际上是我的所有代码,我希望知道这是什么错误以及如何解决它。

最佳答案

对多个元素执行相同的逻辑,点击Array.prototype.forEach.call()

For more better simply do with toggle class instead of checking colours name

for (var i = 1; i < 10; i) {
  var a = document.createElement('h1')
  document.getElementById('div').appendChild(a)
  a.innerHTML = ("HI")
  a.id = "id"
  document.getElementById("id").setAttribute('class', 'a');
  a.id = null
  a.href = ""
  i = i + 1
  var b = document.createElement('h1')
  document.getElementById('div').appendChild(b)
  b.innerHTML = ("BYE")
  b.id = "id"
  document.getElementById("id").setAttribute('class', 'b');
  b.id = null
  b.href = ""
  i = i + 1
}

for (i = 1; i < 10000; i) {
  setTimeout(function() {
    document.getElementsByClassName('a')[0].setAttribute("style", "background-color:red;color:red")
    Array.prototype.forEach.call(document.getElementsByClassName('a'),
      item => item.setAttribute("style", "color: blue;background-color:blue"));
  }, (2 * i - 0) * 1000)
  setTimeout(function() {
    document.getElementsByClassName('a')[0].setAttribute("style", "background-color:blue;color:blue")
    Array.prototype.forEach.call(document.getElementsByClassName('a'),
      item => item.setAttribute("style", "color: red;background-color:red"));
  }, (2 * i - 1) * 1000)
  setTimeout(function() {
    document.getElementsByClassName('b')[0].setAttribute("style", "background-color:blue;color:blue")
    Array.prototype.forEach.call(document.getElementsByClassName('b'),
      item => item.setAttribute("style", "color: blue;background-color:blue"));
  }, (2 * i - 1) * 1000)
  setTimeout(function() {
    document.getElementsByClassName('b')[0].setAttribute("style", "background-color:red;color:red")
    Array.prototype.forEach.call(document.getElementsByClassName('b'),
      item => item.setAttribute("style", "color: red;background-color:red"));
  }, (2 * i - 0) * 1000)
  i = i + 1
}
var color = document.querySelectorAll('h1')
 Array.prototype.forEach.call(color,item=>
item.onclick = function() {

  console.log(this.style.backgroundColor )
  this.classList.toggle('white')
})
.a {
  display: inline;
  background-color: blue;
  color: blue;
  width: 100px;
  float: left;
}

.b {
  display: inline;
  background-color: red;
  color: red;
  width: 100px;
  float: left;
}

h1.white,h1:hover{
background-color: white !important;
  color: white !important;
}
.a::selection {
  display: inline;
  background-color: blue;
  color: blue;
  width: 100px;
  float: left;
}

.b::selection {
  display: inline;
  background-color: red;
  color: red;
  width: 100px;
  float: left;
}
<div id="div" class="div">
</div>

关于javascript - onclick() 函数不运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47013102/

相关文章:

javascript - 通过 WebApi 将媒体批量上传到 Azure Blob 存储

javascript - Opencart Ajax 过滤器 对于产品

javascript - HTML5 : play on speakers stream from microphone on Ubuntu

javascript图像源

java - ImageView onClickListener()

javascript - ScrollintoView 不工作 Javascript

javascript - 在 javaScript 中声明函数时使用的原型(prototype)是什么

css - 如何解决div定位问题

html - CSS 中滚动文本的问题

javascript - 使用类名在 JavaScript 中切换回操作