如何在将鼠标悬停在其他元素上时显示元素
How to display an element on hover over other element
我想在悬停另一个按钮元素时显示一个按钮元素,但我的代码不起作用。
HTML代码
<div class="right-account">
<button class="btn_login">MY ACCOUNT</button>
<a href="../products/user_cart.php"><button class="btn_signup">MY CART</button></a><br>
<button class="pwd_button">Change Password</button>
</div>
CSS代码
.pwd_button{
display: none;
border: 1px solid #13619f;
background: #2371b7;
padding: 5px;
color: white;
font-size: 15px;
}
.btn_login:hover + .pwd_button{
display: block;
}
但是这段代码不起作用。
尝试使用~:
const btn = document.querySelector('.btn_login')
const pwd = document.querySelector('.pwd_button')
btn.addEventListener('mouseover', () => {
pwd.classList.add('showpwd')
})
pwd.addEventListener('mouseleave', () => {
pwd.classList.remove('showpwd')
})
.pwd_button{
display: none;
border: 1px solid #13619f;
background: #2371b7;
padding: 5px;
color: white;
font-size: 15px;
}
.btn_login:hover ~ .pwd_button{
display: block;
}
.showpwd {
display: block;
}
<div class="right-account">
<button class="btn_login">MY ACCOUNT</button>
<a href="../products/user_cart.php">
<button class="btn_signup">MY CART</button>
</a>
<br>
<button class="pwd_button">Change Password</button>
</div>
问题是 + 组合符选择了直接兄弟,而密码按钮不是直接兄弟。但它是一个兄弟所以你可以通过选择 tilda 来做你想做的事 ~
参见MDN:
General sibling combinator
If you want to select siblings of an element even if they are not
directly adjacent, then you can use the general sibling combinator
(~).
所以使用:
.btn_login:hover ~ .pwd_button{
display: block;
}
.pwd_button {
display: none;
border: 1px solid #13619f;
background: #2371b7;
padding: 5px;
color: white;
font-size: 15px;
}
.btn_login:hover~.pwd_button {
display: block;
}
<div class="right-account">
<button class="btn_login">MY ACCOUNT</button>
<a href="../products/user_cart.php"><button class="btn_signup">MY CART</button></a><br>
<button class="pwd_button">Change Password</button>
</div>
或者我们可以用另一种方式来做 :-D :-P =>
.pwd_button{
display: none;
border: 1px solid #13619f;
background: #2371b7;
padding: 5px;
color: white;
font-size: 15px;
}
.btn_login:hover + .pwd_button{
/*display: inline-block;*/
display: block;
}
<div class="right-account">
<button class="btn_login">MY ACCOUNT</button>
<button class="pwd_button">Change Password</button>
<a href="../products/user_cart.php"><button class="btn_signup">MY CART</button></a><br>
</div>
我想在悬停另一个按钮元素时显示一个按钮元素,但我的代码不起作用。
HTML代码
<div class="right-account">
<button class="btn_login">MY ACCOUNT</button>
<a href="../products/user_cart.php"><button class="btn_signup">MY CART</button></a><br>
<button class="pwd_button">Change Password</button>
</div>
CSS代码
.pwd_button{
display: none;
border: 1px solid #13619f;
background: #2371b7;
padding: 5px;
color: white;
font-size: 15px;
}
.btn_login:hover + .pwd_button{
display: block;
}
但是这段代码不起作用。
尝试使用~:
const btn = document.querySelector('.btn_login')
const pwd = document.querySelector('.pwd_button')
btn.addEventListener('mouseover', () => {
pwd.classList.add('showpwd')
})
pwd.addEventListener('mouseleave', () => {
pwd.classList.remove('showpwd')
})
.pwd_button{
display: none;
border: 1px solid #13619f;
background: #2371b7;
padding: 5px;
color: white;
font-size: 15px;
}
.btn_login:hover ~ .pwd_button{
display: block;
}
.showpwd {
display: block;
}
<div class="right-account">
<button class="btn_login">MY ACCOUNT</button>
<a href="../products/user_cart.php">
<button class="btn_signup">MY CART</button>
</a>
<br>
<button class="pwd_button">Change Password</button>
</div>
问题是 + 组合符选择了直接兄弟,而密码按钮不是直接兄弟。但它是一个兄弟所以你可以通过选择 tilda 来做你想做的事 ~
参见MDN:
General sibling combinator If you want to select siblings of an element even if they are not directly adjacent, then you can use the general sibling combinator (~).
所以使用:
.btn_login:hover ~ .pwd_button{
display: block;
}
.pwd_button {
display: none;
border: 1px solid #13619f;
background: #2371b7;
padding: 5px;
color: white;
font-size: 15px;
}
.btn_login:hover~.pwd_button {
display: block;
}
<div class="right-account">
<button class="btn_login">MY ACCOUNT</button>
<a href="../products/user_cart.php"><button class="btn_signup">MY CART</button></a><br>
<button class="pwd_button">Change Password</button>
</div>
或者我们可以用另一种方式来做 :-D :-P =>
.pwd_button{
display: none;
border: 1px solid #13619f;
background: #2371b7;
padding: 5px;
color: white;
font-size: 15px;
}
.btn_login:hover + .pwd_button{
/*display: inline-block;*/
display: block;
}
<div class="right-account">
<button class="btn_login">MY ACCOUNT</button>
<button class="pwd_button">Change Password</button>
<a href="../products/user_cart.php"><button class="btn_signup">MY CART</button></a><br>
</div>