3 回答

TA貢獻1773條經驗 獲得超3個贊
我可以看到的直接問題是你有3個具有相同ID的div。ID 應該是唯一的。將 id 更改為類,并將 javascript 引用更改為類:
let text = document.querySelectorAll(".text");

TA貢獻1842條經驗 獲得超13個贊
我得到了我的答案,感謝任何幫助過我的人
jQuery
$('.text').hide();
$(".boxcontainer a").on('mouseenter', function() {
$(this).find('.text').fadeIn(300);
});
$(".boxcontainer a").on('mouseleave',function() {
$(this).find('.text').hide();
});
斷續器
<div class="boxcontainer">
<a href="asia.html" style="background-color:#46515a;"> <div class = 'text'>asia.</div></a>
<a href="americas.html" style="background-color: #2d343a;"> <div class='text'>americas.</div></a>
<a href="europe.html" style="background-color: #1a1f22;"><div class='text'>europe.</div></a>
</div>
斷續器
.boxcontainer{
width: 30%;
height: 50%;
position: fixed;
display: flex;
flex-direction: column;
z-index: 100;
}
.boxcontainer a{
box-shadow: -9px 12px 7px 0px rgba(0, 0, 0, 0.336);
margin: 10px;
padding-top: 10%;
padding-bottom: 10%;
text-decoration: none;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.boxcontainer a:hover{
transform: scale(1.1);
color: white;
}

TA貢獻1900條經驗 獲得超5個贊
您正在 DOM 訪問上使用 jQuery 功能。
你不能將鼠標懸停在隱藏的東西上,所以我的答案將div移出錨點。
正如J Doe所指出的,文本ID應該更改為類,因為ID需要是唯一的 - 這與實際問題無關。
這是vanilla版本 - 我通常會委托,但在這種情況下,我們需要在每個鏈接上都有一個eventListener
const overAndOut = e => {
const tgt = e.target;
if (tgt.tagName === "A") {
tgt.nextElementSibling.classList.toggle('text', e.type === "mouseleave")
}
};
[...document.querySelectorAll('#boxcontainer a')].forEach(link => {
link.addEventListener('mouseover', overAndOut)
link.addEventListener('mouseleave', overAndOut)
})
.boxcontainer {
width: 30%;
height: 50%;
position: fixed;
display: flex;
flex-direction: column;
z-index: 100;
}
.boxcontainer a {
box-shadow: -9px 12px 7px 0px rgba(0, 0, 0, 0.336);
margin: 10px;
padding-top: 10%;
padding-bottom: 10%;
text-decoration: none;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.boxcontainer a:hover {
transform: scale(1.1);
color: white;
}
.text {
visibility: hidden;
}
<div id="boxcontainer">
<a href="asia.html" style="background-color:#46515a;">Asia</a>
<div class='text'>asia.</div>
<a href="americas.html" style="background-color: #2d343a;">Americas</a>
<div class='text'>Americas.</div>
<a href="europe.html" style="background-color: #1a1f22;">Europe</a>
<div class='text'>Europe.</div>
</div>
jQuery:
$('#boxcontainer a').on("mouseenter", function() {
$(this).next().removeClass("text")
})
.on("mouseleave", function() {
$(this).next().addClass("text")
});
.boxcontainer {
width: 30%;
height: 50%;
position: fixed;
display: flex;
flex-direction: column;
z-index: 100;
}
.boxcontainer a {
box-shadow: -9px 12px 7px 0px rgba(0, 0, 0, 0.336);
margin: 10px;
padding-top: 10%;
padding-bottom: 10%;
text-decoration: none;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.boxcontainer a:hover {
transform: scale(1.1);
color: white;
}
.text {
visibility: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="boxcontainer">
<a href="asia.html" style="background-color:#46515a;">Asia</a>
<div class='text'>Asia.</div>
<a href="americas.html" style="background-color: #2d343a;">Americas</a>
<div class='text'>Americas.</div>
<a href="europe.html" style="background-color: #1a1f22;">Europe</a>
<div class='text'>Europe.</div>
</div>
添加回答
舉報