gpt4 book ai didi

javascript - AddEventListener 仅使用 Id 而不是类

转载 作者:行者123 更新时间:2023-11-30 08:00:51 24 4
gpt4 key购买 nike

尝试在单击第一个按钮时创建一个白框。奇怪的是,它仅在我将偶数处理程序附加到 ID 时才有效。而不是上课。我要么将 addEventListener 与 Class 一起使用,要么以错误的方式调用 Class。欢迎指正,谢谢!

这是在使用 ID

document.addEventListener('DOMContentLoaded',domloaded,false);
function domloaded(){
var bttn = document.getElementById('bttn');


bttn.addEventListener('click', draw, true);

function draw(){

var box = document.getElementById('box');
box.style.background = 'white';
box.style.display = 'block';
}

}
/* line 5, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font: inherit;
font-size: 100%;
vertical-align: baseline;
}

/* line 22, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
html {
line-height: 1;
}

/* line 24, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
ol, ul {
list-style: none;
}

/* line 26, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
table {
border-collapse: collapse;
border-spacing: 0;
}

/* line 28, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
caption, th, td {
text-align: left;
font-weight: normal;
vertical-align: middle;
}

/* line 30, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
q, blockquote {
quotes: none;
}
/* line 103, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
q:before, q:after, blockquote:before, blockquote:after {
content: "";
content: none;
}

/* line 32, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
a img {
border: none;
}

/* line 116, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
article, aside, details, figcaption, figure, footer, header, hgroup, main, menu, nav, section, summary {
display: block;
}

/* line 3, ../sass/main.scss */
body {
background-color: darkred;
width: 100%;
height: 100%;
}

/* line 9, ../sass/main.scss */
button {
float: left;
width: 50%;
height: 10%;
background: yellow;
}

/* line 16, ../sass/main.scss */
#box {
float: left;
width: 50%;
height: 70%;
background: blue;
display: none;
}

/* line 24, ../sass/main.scss */
#drawing_container {
position: absolute;
margin: auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 300px;
height: 300px;
background-color: black;
color: white;
}
<body>
<div id="drawing_container">
<button id="bttn"></button>
<button id="bttn"></button>
<div id="box"></div>
</div>
</body>

这是在使用类

document.addEventListener('DOMContentLoaded',domloaded,false);
function domloaded(){
var bttn = document.getElementsByClassName('bttn');


bttn.addEventListener('click', draw, true);

function draw(){

var box = document.getElementById('box');
box.style.background = 'white';
box.style.display = 'block';
}

}
/* line 5, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font: inherit;
font-size: 100%;
vertical-align: baseline;
}

/* line 22, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
html {
line-height: 1;
}

/* line 24, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
ol, ul {
list-style: none;
}

/* line 26, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
table {
border-collapse: collapse;
border-spacing: 0;
}

/* line 28, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
caption, th, td {
text-align: left;
font-weight: normal;
vertical-align: middle;
}

/* line 30, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
q, blockquote {
quotes: none;
}
/* line 103, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
q:before, q:after, blockquote:before, blockquote:after {
content: "";
content: none;
}

/* line 32, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
a img {
border: none;
}

/* line 116, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.1/stylesheets/compass/reset/_utilities.scss */
article, aside, details, figcaption, figure, footer, header, hgroup, main, menu, nav, section, summary {
display: block;
}

/* line 3, ../sass/main.scss */
body {
background-color: darkred;
width: 100%;
height: 100%;
}

/* line 9, ../sass/main.scss */
button {
float: left;
width: 50%;
height: 10%;
background: yellow;
}

/* line 16, ../sass/main.scss */
.box {
float: left;
width: 50%;
height: 70%;
background: blue;
display: none;
}

/* line 24, ../sass/main.scss */
#drawing_container {
position: absolute;
margin: auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 300px;
height: 300px;
background-color: black;
color: white;
}
<body>
<div id="drawing_container">
<button class="bttn"></button>
<button class="bttn"></button>
<div id="box"></div>
</div>
</body>

再一次,我永远的感谢!

最佳答案

在调用 getElementByClassName 时需要迭代元素数组,因为它返回元素数组。

document.addEventListener('DOMContentLoaded',domloaded,false);
function domloaded(){


var bttn = document.getElementsByClassName('bttn');

for(var i = 0; i < bttn.length; i++) {
bttn[i].addEventListener("click", draw, true);
}


function draw(){

var box = document.getElementById('box');
box.style.background = 'white';
box.style.display = 'block';
}

}

关于javascript - AddEventListener 仅使用 Id 而不是类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28688491/

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