gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:02:08 24 4
gpt4 key购买 nike

我尝试了多种方法来执行此操作,但 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com