英文:
How to select multiple elements with queryselector
问题
I'm trying to make something that the user selects a color with the first input, and it changes the background color of the body. When the user selects a color in the "Subtitles color" input, it should change the color of both
elements, but the second
isn't changing the color. Can someone help me? I have tried using getElementById and getElementsByClassName, and it won't even select the first child.
英文:
I'm trying to make something that the user select a color with the first input and it changes the background color of the body, and when the user select a color in the "Subtitles color" input it should change the color of both <h2>, but the second <h2> isn't changing the color. Can someone help me? I have tried using getElementById and getElementsByClassName and it won't even select the first child.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const ColorPicker = document.querySelector("#input1");
const ColorCode = document.querySelector("#input1").value;
const Body = document.querySelector("#fundoPagina");
const ColorPicker4 = document.querySelector("#input4");
const ColorCode4 = document.querySelector("#input4").value;
const subtitulo = document.querySelector("h2");
// Change color function
const changeColor = (evt) => {
const selectedColor = evt.currentTarget.value;
ColorCode.value = selectedColor;
Body.style.backgroundColor = selectedColor;
};
const changeColor4 = (evt) => {
const selectedColor4 = evt.currentTarget.value;
ColorCode4.value = selectedColor4;
subtitulo.style.color = selectedColor4;
};
// Add the Event to your input
ColorPicker .addEventListener("input", changeColor);
ColorPicker4 .addEventListener("input", changeColor4);
<!-- language: lang-css -->
.inputContent {
display: flex;
text-align:center;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
border: 2px solid rgb(66, 66, 66);
max-width: 890px;
margin: 0 auto;
padding: 5px 15px;
background: rgb(219, 219, 219);
}
#inputColor {
display: flex;
flex-direction: column!important;
align-items: center;
}
#inputColor>p {
font-size: 12px;
}
#fundoCaixa {
max-width: 890px;
margin: 0 auto;
border: 2px solid rgb(66, 66, 66);
margin-top: 15px;
background: rgb(89, 156, 187);
padding: 5px 15px;
}
<!-- language: lang-html -->
<body id="fundoPagina">
<div class="inputContent">
<div id="inputColor"><input type="color" value="#000" id="input1"></input><p>Cor do fundo da página</p></div>
<div id="inputColor"><input type="color" value="#000" id="input4"></input><p>Subtitles Color</p></div>
</div>
<main id="fundoCaixa">
<h1 id="tituloPrincipal">Linguagens de Programação</h1>
<p id="paragrafo">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum nihil incidunt sint laudantium est earum optio ducimus in facilis quisquam. Tempore iste dignissimos.</p>
<h2 id="linguagemC" class="subtitulo">Linguagem C</h2>
<h2 id="linguagemPascal" class="subtitulo">Linguagem Pascal</h2>
</main>
</body>
<!-- end snippet -->
I'm trying to make something that the user select a color with the first input and it changes the background color of the body, and when the user select a color in the "Subtitles color" input it should change the color of both <h2>, but the second <h2> isn't changing the color. Can someone help me? I have tried using getElementById and getElementsByClassName and it won't even select the first child.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const ColorPicker = document.querySelector("#input1");
const ColorCode = document.querySelector("#input1").value;
const Body = document.querySelector("#fundoPagina");
const ColorPicker4 = document.querySelector("#input4");
const ColorCode4 = document.querySelector("#input4").value;
const subtitulo = document.querySelector("h2");
// Change color function
const changeColor = (evt) => {
const selectedColor = evt.currentTarget.value;
ColorCode.value = selectedColor;
Body.style.backgroundColor = selectedColor;
};
const changeColor4 = (evt) => {
const selectedColor4 = evt.currentTarget.value;
ColorCode4.value = selectedColor4;
subtitulo.style.color = selectedColor4;
};
// Add the Event to your input
ColorPicker .addEventListener("input", changeColor);
ColorPicker4 .addEventListener("input", changeColor4);
<!-- language: lang-css -->
.inputContent {
display: flex;
text-align:center;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
border: 2px solid rgb(66, 66, 66);
max-width: 890px;
margin: 0 auto;
padding: 5px 15px;
background: rgb(219, 219, 219);
}
#inputColor {
display: flex;
flex-direction: column!important;
align-items: center;
}
#inputColor>p {
font-size: 12px;
}
#fundoCaixa {
max-width: 890px;
margin: 0 auto;
border: 2px solid rgb(66, 66, 66);
margin-top: 15px;
background: rgb(89, 156, 187);
padding: 5px 15px;
}
<!-- language: lang-html -->
<body id="fundoPagina">
<div class="inputContent">
<div id="inputColor"><input type="color" value="#000" id="input1"></input><p>Cor do fundo da página</p></div>
<div id="inputColor"><input type="color" value="#000" id="input4"></input><p>Subtitles Color</p></div>
</div>
<main id="fundoCaixa">
<h1 id="tituloPrincipal">Linguagens de Programação</h1>
<p id="paragrafo">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum nihil incidunt sint laudantium est earum optio ducimus in facilis quisquam. Tempore iste dignissimos.</p>
<h2 id="linguagemC" class="subtitulo">Linguagem C</h2>
<h2 id="linguagemPascal" class="subtitulo">Linguagem Pascal</h2>
</main>
</body>
<!-- end snippet -->
答案1
英文:
I'm trying to make something that the user select a color with the first input and it changes the background color of the body, and when the user select a color in the "Subtitles color" input it should change the color of both <h2>, but the second <h2> isn't changing the color. Can someone help me? I have tried using getElementById and getElementsByClassName and it won't even select the first child.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const ColorPicker = document.querySelector("#input1");
const ColorCode = document.querySelector("#input1").value;
const Body = document.querySelector("#fundoPagina");
const ColorPicker4 = document.querySelector("#input4");
const ColorCode4 = document.querySelector("#input4").value;
const subtitulo = document.querySelector("h2");
// Change color function
const changeColor = (evt) => {
const selectedColor = evt.currentTarget.value;
ColorCode.value = selectedColor;
Body.style.backgroundColor = selectedColor;
};
const changeColor4 = (evt) => {
const selectedColor4 = evt.currentTarget.value;
ColorCode4.value = selectedColor4;
subtitulo.style.color = selectedColor4;
};
// Add the Event to your input
ColorPicker .addEventListener("input", changeColor);
ColorPicker4 .addEventListener("input", changeColor4);
<!-- language: lang-css -->
.inputContent {
display: flex;
text-align:center;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
border: 2px solid rgb(66, 66, 66);
max-width: 890px;
margin: 0 auto;
padding: 5px 15px;
background: rgb(219, 219, 219);
}
#inputColor {
display: flex;
flex-direction: column!important;
align-items: center;
}
#inputColor>p {
font-size: 12px;
}
#fundoCaixa {
max-width: 890px;
margin: 0 auto;
border: 2px solid rgb(66, 66, 66);
margin-top: 15px;
background: rgb(89, 156, 187);
padding: 5px 15px;
}
<!-- language: lang-html -->
<body id="fundoPagina">
<div class="inputContent">
<div id="inputColor"><input type="color" value="#000" id="input1"></input><p>Cor do fundo da página</p></div>
<div id="inputColor"><input type="color" value="#000" id="input4"></input><p>Subtitles Color</p></div>
</div>
<main id="fundoCaixa">
<h1 id="tituloPrincipal">Linguagens de Programação</h1>
<p id="paragrafo">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum nihil incidunt sint laudantium est earum optio ducimus in facilis quisquam. Tempore iste dignissimos.</p>
<h2 id="linguagemC" class="subtitulo">Linguagem C</h2>
<h2 id="linguagemPascal" class="subtitulo">Linguagem Pascal</h2>
</main>
</body>
<!-- end snippet -->
I'm trying to make something that the user select a color with the first input and it changes the background color of the body, and when the user select a color in the "Subtitles color" input it should change the color of both <h2>, but the second <h2> isn't changing the color. Can someone help me? I have tried using getElementById and getElementsByClassName and it won't even select the first child.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const ColorPicker = document.querySelector("#input1");
const ColorCode = document.querySelector("#input1").value;
const Body = document.querySelector("#fundoPagina");
const ColorPicker4 = document.querySelector("#input4");
const ColorCode4 = document.querySelector("#input4").value;
const subtitulo = document.querySelector("h2");
// Change color function
const changeColor = (evt) => {
const selectedColor = evt.currentTarget.value;
ColorCode.value = selectedColor;
Body.style.backgroundColor = selectedColor;
};
const changeColor4 = (evt) => {
const selectedColor4 = evt.currentTarget.value;
ColorCode4.value = selectedColor4;
subtitulo.style.color = selectedColor4;
};
// Add the Event to your input
ColorPicker .addEventListener("input", changeColor);
ColorPicker4 .addEventListener("input", changeColor4);
<!-- language: lang-css -->
.inputContent {
display: flex;
text-align:center;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
border: 2px solid rgb(66, 66, 66);
max-width: 890px;
margin: 0 auto;
padding: 5px 15px;
background: rgb(219, 219, 219);
}
#inputColor {
display: flex;
flex-direction: column!important;
align-items: center;
}
#inputColor>p {
font-size: 12px;
}
#fundoCaixa {
max-width: 890px;
margin: 0 auto;
border: 2px solid rgb(66, 66, 66);
margin-top: 15px;
background: rgb(89, 156, 187);
padding: 5px 15px;
}
<!-- language: lang-html -->
<body id="fundoPagina">
<div class="inputContent">
<div id="inputColor"><input type="color" value="#000" id="input1"></input><p>Cor do fundo da página</p></div>
<div id="inputColor"><input type="color" value="#000" id="input4"></input><p>Subtitles Color</p></div>
</div>
<main id="fundoCaixa">
<h1 id="tituloPrincipal">Linguagens de Programação</h1>
<p id="paragrafo">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum nihil incidunt sint laudantium est earum optio ducimus in facilis quisquam. Tempore iste dignissimos.</p>
<h2 id="linguagemC" class="subtitulo">Linguagem C</h2>
<h2 id="linguagemPascal" class="subtitulo">Linguagem Pascal</h2>
</main>
</body>
<!-- end snippet -->
答案1
得分: 1
尝试使用 querySelectorAll
querySelectorAll
将获取一个节点列表,使用 forEach
来更改所有选定元素的样式。
实际上,您只需要一个函数来实现这一点,您可以通过 <input/>
中的 data-attribute
更改样式属性和选定的元素。
const colorPickers = document.querySelectorAll('input')
colorPickers.forEach(colorPicker => {
colorPicker.addEventListener('input', ({
target
}) => {
const value = target.value
const elements = target.getAttribute('data-element')
const style = target.getAttribute('data-style')
document.querySelectorAll(elements).forEach(elem => {
elem.style[style] = value
})
})
})
.inputContent {
display: flex;
text-align: center;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
border: 2px solid rgb(66, 66, 66);
max-width: 890px;
margin: 0 auto;
padding: 5px 15px;
background: rgb(219, 219, 219);
}
#inputColor {
display: flex;
flex-direction: column !important;
align-items: center;
}
#inputColor>p {
font-size: 12px;
}
#fundoCaixa {
max-width: 890px;
margin: 0 auto;
border: 2px solid rgb(66, 66, 66);
margin-top: 15px;
background: rgb(89, 156, 187);
padding: 5px 15px;
}
<body id="fundoPagina">
<div class="inputContent">
<div><input type="color" value="#000" data-element="body" data-style="backgroundColor" />
<p>Cor do fundo da página</p>
</div>
<div><input type="color" value="#000" data-element="h2" data-style="color" />
<p>Subtitles Color</p>
</div>
</div>
<main id="fundoCaixa">
<h1 id="tituloPrincipal">Linguagens de Programação</h1>
<p id="paragrafo">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum nihil incidunt sint laudantium est earum optio ducimus in facilis quisquam. Tempore iste dignissimos.</p>
<h2 id="linguagemC" class="subtitulo">Linguagem C</h2>
<h2 id="linguagemPascal" class="subtitulo">Linguagem Pascal</h2>
</main>
</body>
英文:
Try querySelectorAll
querySelectorAll
will get a nodelist, use foreach
to change style for all of selected elements.
And you actually only need one function to do this, you can change the style attribute and selected element by data-attribute
in <input/>
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const colorPickers = document.querySelectorAll('input')
colorPickers.forEach(colorPicker => {
colorPicker.addEventListener('input', ({
target
}) => {
const value = target.value
const elements = target.getAttribute('data-element')
const style = target.getAttribute('data-style')
document.querySelectorAll(elements).forEach(elem => {
elem.style[style] = value
})
})
})
<!-- language: lang-css -->
.inputContent {
display: flex;
text-align: center;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
border: 2px solid rgb(66, 66, 66);
max-width: 890px;
margin: 0 auto;
padding: 5px 15px;
background: rgb(219, 219, 219);
}
#inputColor {
display: flex;
flex-direction: column !important;
align-items: center;
}
#inputColor>p {
font-size: 12px;
}
#fundoCaixa {
max-width: 890px;
margin: 0 auto;
border: 2px solid rgb(66, 66, 66);
margin-top: 15px;
background: rgb(89, 156, 187);
padding: 5px 15px;
}
<!-- language: lang-html -->
<body id="fundoPagina">
<div class="inputContent">
<div><input type="color" value="#000" data-element="body" data-style="backgroundColor" />
<p>Cor do fundo da página</p>
</div>
<div><input type="color" value="#000" data-element="h2" data-style="color" />
<p>Subtitles Color</p>
</div>
</div>
<main id="fundoCaixa">
<h1 id="tituloPrincipal">Linguagens de Programação</h1>
<p id="paragrafo">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum nihil incidunt sint laudantium est earum optio ducimus in facilis quisquam. Tempore iste dignissimos.</p>
<h2 id="linguagemC" class="subtitulo">Linguagem C</h2>
<h2 id="linguagemPascal" class="subtitulo">Linguagem Pascal</h2>
</main>
</body>
<!-- end snippet -->
答案2
得分: 0
我在你的代码中进行了一些小的更改。这些更改用<-- edit
标记出来了。
const ColorPicker = document.querySelector("#input1");
const ColorCode = document.querySelector("#input1").value;
const Body = document.querySelector("#fundoPagina");
const ColorPicker4 = document.querySelector("#input4");
const ColorCode4 = document.querySelector("#input4").value;
const subtitulo = document.querySelectorAll("h2"); // <-- edit
const changeColor = (evt) => {
const selectedColor = evt.currentTarget.value;
ColorCode.value = selectedColor;
Body.style.backgroundColor = selectedColor;
};
const changeColor4 = (evt) => {
const selectedColor4 = evt.currentTarget.value;
ColorCode4.value = selectedColor4;
subtitulo.forEach((h) => (h.style.color = selectedColor4)); // <-- edit
};
// 将事件添加到你的输入元素
ColorPicker.addEventListener("input", changeColor);
ColorPicker4.addEventListener("input", changeColor4);
.inputContent {
display: flex;
text-align: center;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
border: 2px solid rgb(66, 66, 66);
max-width: 890px;
margin: 0 auto;
padding: 5px 15px;
background: rgb(219, 219, 219);
}
#inputColor {
display: flex;
flex-direction: column !important;
align-items: center;
}
#inputColor > p {
font-size: 12px;
}
#fundoCaixa {
max-width: 890px;
margin: 0 auto;
border: 2px solid rgb(66, 66, 66);
margin-top: 15px;
background: rgb(89, 156, 187);
padding: 5px 15px;
}
<body id="fundoPagina">
<div class="inputContent">
<div id="inputColor"><input type="color" value="#000" id="input1"></input><p>Cor do fundo da página</p></div>
<div id="inputColor"><input type="color" value="#000" id="input4"></input><p>Subtitles Color</p></div>
</div>
<main id="fundoCaixa">
<h1 id="tituloPrincipal">Linguagens de Programação</h1>
<p id="paragrafo">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum nihil incidunt sint laudantium
est earum optio ducimus in facilis quisquam. Tempore iste dignissimos.</p>
<h2 id="linguagemC" class="subtitulo">Linguagem C</h2>
<h2 id="linguagemPascal" class="subtitulo">Linguagem Pascal</h2>
</main>
</body>
如果你需要进一步的帮助,请告诉我。
英文:
I made some minor change in your code.
that change highlighted with <-- edit
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const ColorPicker = document.querySelector("#input1");
const ColorCode = document.querySelector("#input1").value;
const Body = document.querySelector("#fundoPagina");
const ColorPicker4 = document.querySelector("#input4");
const ColorCode4 = document.querySelector("#input4").value;
const subtitulo = document.querySelectorAll("h2"); //<-- edit
const changeColor = (evt) => {
const selectedColor = evt.currentTarget.value;
ColorCode.value = selectedColor;
Body.style.backgroundColor = selectedColor;
};
const changeColor4 = (evt) => {
const selectedColor4 = evt.currentTarget.value;
ColorCode4.value = selectedColor4;
subtitulo.forEach( h => h.style.color = selectedColor4 ) //<-- edit
};
// Add the Event to your input
ColorPicker .addEventListener("input", changeColor);
ColorPicker4 .addEventListener("input", changeColor4);
<!-- language: lang-css -->
.inputContent {
display: flex;
text-align:center;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
border: 2px solid rgb(66, 66, 66);
max-width: 890px;
margin: 0 auto;
padding: 5px 15px;
background: rgb(219, 219, 219);
}
#inputColor {
display: flex;
flex-direction: column!important;
align-items: center;
}
#inputColor>p {
font-size: 12px;
}
#fundoCaixa {
max-width: 890px;
margin: 0 auto;
border: 2px solid rgb(66, 66, 66);
margin-top: 15px;
background: rgb(89, 156, 187);
padding: 5px 15px;
}
<!-- language: lang-html -->
<body id="fundoPagina">
<div class="inputContent">
<div id="inputColor"><input type="color" value="#000" id="input1"></input><p>Cor do fundo da página</p></div>
<div id="inputColor"><input type="color" value="#000" id="input4"></input><p>Subtitles Color</p></div>
</div>
<main id="fundoCaixa">
<h1 id="tituloPrincipal">Linguagens de Programação</h1>
<p id="paragrafo">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ipsum nihil incidunt sint laudantium est earum optio ducimus in facilis quisquam. Tempore iste dignissimos.</p>
<h2 id="linguagemC" class="subtitulo">Linguagem C</h2>
<h2 id="linguagemPascal" class="subtitulo">Linguagem Pascal</h2>
</main>
</body>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论