簡體   English   中英

使用jQuery懸停使圖像消失

[英]Make an image in an li disappear with hover using jQuery

當鼠標懸停在元素上並顯示鼠標離開元素時,嘗試在li中隱藏img 我后來想要一個段落顯示li類的名稱來代替圖像,但我想專注於隱藏圖像。

我已經搞砸了一段時間了,即使在查看與此相關的其他帖子之后,我似乎無法弄清楚出了什么問題。

<ul id="language">
    <li class="a"><img src="img/a.png" alt="a"></li>
    <li class="b"><img src="img/b.png" alt="b"></li>
</ul>
<script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script>
<script src="nameDisplay.js" type = "text/javascript" charset="utf-8"></script>

在nameDisplay.js中

$('#language li').hover(function(){
    $(this 'img').hide();
}, function(){
    $(this 'img').show();
});

只需使用css,無需使用jQuery

#language li:hover img{
    display: none;
}

 $(function() { $('#language li').hover(function() { $('img', this).hide(); // You can either of these // $(this).find('img') // $(this).children('img') }, function() { $('img', this).show(); }); }); 
 <script src="//code.jquery.com/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script> <ul id="language"> <li class="a"> <img src="img/a.png" alt="a"> </li> <li class="b"> <img src="img/b.png" alt="b"> </li> </ul> 

您找到圖像的選擇器不正確。 您可以使用上下文選擇器或.find()children()方法

$(function() {
    $('#language li').hover(function(){
        $('img', this).hide();
        // You can either of these 
        // $(this).find('img') 
        // $(this).children('img') 
    }, function(){
        $('img', this).show();
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM