admin管理员组文章数量:1355614
I'm trying to have an icon change color everytime an input box is hovered. The problem is it is only changing the background when hovering the first input textbox, not the other two.
I'm trying to get this effect without jquery.Thanks in advance for your help !
var input = document.querySelector ('input');
var icon = document.querySelector ('img');
input.addEventListener ('mouseover', function hover(){
icon.style.background = '#D46855';
});
input.addEventListener ('mouseleave', function leave(){
icon.style.background = '#81D455';
});
@import url('+Flower');
body{
background-image :url(.565449774c2d4.png);
}
h1{
font-family: 'Indie Flower', cursive;
font-size:3em;
color:#D46855;
letter-spacing:5px;
}
#container{
text-align:center;
margin:10% auto;
padding-top: 50px;
border-radius:15%;
width:80%;
height:400px;
position:relative;
background-color : rgba(84, 193, 212, 0.56);
box-shadow: 5px 5px 4px 5px rgba(84, 193, 212, 0.56);
}
img{
left: 0;
right: 0;
bottom: 0;
margin: auto;
height:100px;
width : 100px;
position:absolute;
top: -95%;
border-radius:30px;
background:#81D455;
}
input{
width:200px;
height:15px;
font-size:1.2em;
padding:10px;
text-align:center;
display:flex;
margin:0 auto;
margin-top:20px;
}
<div id = 'container'>
<h1>To Do List</h1>
<img src = ".png">
<form>
<input type = 'text' placeholder = 'item' id = 'list' </input>
<input type = 'text' placeholder = 'item' id = 'list' </input>
<input type = 'text' placeholder = 'item' id = 'list' </input>
</form>
</div>
</body>
I'm trying to have an icon change color everytime an input box is hovered. The problem is it is only changing the background when hovering the first input textbox, not the other two.
I'm trying to get this effect without jquery.Thanks in advance for your help !
var input = document.querySelector ('input');
var icon = document.querySelector ('img');
input.addEventListener ('mouseover', function hover(){
icon.style.background = '#D46855';
});
input.addEventListener ('mouseleave', function leave(){
icon.style.background = '#81D455';
});
@import url('https://fonts.googleapis./css?family=Indie+Flower');
body{
background-image :url(https://mir-s3-cdn-cf.behance/project_modules/max_1200/b0e53331516629.565449774c2d4.png);
}
h1{
font-family: 'Indie Flower', cursive;
font-size:3em;
color:#D46855;
letter-spacing:5px;
}
#container{
text-align:center;
margin:10% auto;
padding-top: 50px;
border-radius:15%;
width:80%;
height:400px;
position:relative;
background-color : rgba(84, 193, 212, 0.56);
box-shadow: 5px 5px 4px 5px rgba(84, 193, 212, 0.56);
}
img{
left: 0;
right: 0;
bottom: 0;
margin: auto;
height:100px;
width : 100px;
position:absolute;
top: -95%;
border-radius:30px;
background:#81D455;
}
input{
width:200px;
height:15px;
font-size:1.2em;
padding:10px;
text-align:center;
display:flex;
margin:0 auto;
margin-top:20px;
}
<div id = 'container'>
<h1>To Do List</h1>
<img src = "https://www.jamiesale-cartoonist./wp-content/uploads/cartoon-business-man-free1.png">
<form>
<input type = 'text' placeholder = 'item' id = 'list' </input>
<input type = 'text' placeholder = 'item' id = 'list' </input>
<input type = 'text' placeholder = 'item' id = 'list' </input>
</form>
</div>
</body>
Any advice would be appreciated (:
Share Improve this question asked Sep 29, 2017 at 3:20 SwinkSwink 4575 silver badges33 bronze badges 2-
document.querySelector
returns one single element, the first that matches the supplied selector - you needdocument.querySelectorAll
and iterate through the result (disregard CSS solution) – Jaromanda X Commented Sep 29, 2017 at 3:22 - Well you only put an event listener on 1 of the inputs, you need to put the event on all of them, or use event delegation – Patrick Evans Commented Sep 29, 2017 at 3:22
1 Answer
Reset to default 9querySelector
returns only the first element that is found.
As you have multiple input
elements use querySelectorAll
var input = document.querySelectorAll('input');
Also as querySelectorAll
returns multiple elements ( which is a node list of inputs in this case ) , you will have to attach the event handlers in a loop.
var inputs = document.querySelectorAll('input');
var icon = document.querySelector('img');
inputs.forEach(function(input) {
input.addEventListener('mouseover', function hover() {
icon.style.background = '#D46855';
});
input.addEventListener('mouseleave', function leave() {
icon.style.background = '#81D455';
});
});
本文标签: htmlJavascript mouseover only working on first elementStack Overflow
版权声明:本文标题:html - Javascript mouseover only working on first element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744032104a2579100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论