简体   繁体   English

如何使用鼠标悬停(或悬停)以便在 go 在不同的文本上显示不同的图像? JAVASCRIPT

[英]How do I use mouseover (or hover) so that I can display different images when I go over different texts? JAVASCRIPT

I'm currently doing CS50 and I need to write a little html page inserting interactive elements from Javascript.I'm a total beginner with this language and I'm trying to find out how to display an image when I hover on a text.我目前正在做 CS50,我需要编写一个小 html 页面,插入来自 Javascript 的交互式元素。我完全是这种语言的初学者,我正在尝试找出如何在我 ZE0542F579DF58E81238ZADEF9 上显示一个文本时显示图像。 Apparently there's almost zero topic about this on internet, I've only found this,which does work but only with the first image.The reason is because the "id" is the same for all the urls,I get that,but how can I modify the code so that it can work for all the different texts/pictures?I kind of get the whole picture because I know Python but html and Js are brand new to me.Any help would be appreciated,thanks显然,互联网上关于这个的话题几乎为零,我只发现了这个,它确实有效,但只适用于第一张图片。原因是因为所有网址的“id”都是相同的,我明白了,但怎么能我修改了代码,以便它适用于所有不同的文本/图片?我有点了解全貌,因为我知道 Python 但 html 和 Js 对我来说是全新的。任何帮助将不胜感激,谢谢

 <div>
 <table>
   <tr>
     <th>Best Albums</th>
     <th>Year</th>
       </tr>
       <tr>
         <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/2Lm375yVwwGyAHfNFP2HU6-970-80.jpg.webp"/> <span>Spreading the disease</span></td>
     <td>1985</td>
       </tr>
       <tr>
         <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/jP4Fx9doBk4R27kg5hpsrd-970-80.jpg.webp"/> <span>Among the living</span></td>
         <td>1987</td>
       </tr>
        <tr>
        <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/sj49Yj3eJERgwwVbZBZ5nJ-970-80.jpg.webp"/> <span>Persistence of time</span></td>
                <td>1990</td>
        </tr>
        <tr>
          <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/BhDq2Bdm7axUP9erRcs5i9-970-80.jpg.webp"/> <span>Sound of white noise</span></td>
           <td>1993</td>
        </tr>
        <tr>
          <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/fKVNiGMLbFtsXt6uC8aLMm-970-80.jpg.webp"/> <span>We've come for you all</span></td>
           <td>2003</td>
        </tr>
         
        </table>
        <script>
            $(document).ready(function () {
            $('span').hover(function(){
                $(this).addClass('underline');
                $('#image').show();
            },function(){
                $(this).removeClass('underline');
                $('#image').hide();
            });
        });</script>
    </div>
    
    <br><a href="scott.html"><button type="button">Check them out</button></a><br>
    
    </body>

ids have to be unique. id 必须是唯一的。 So you need to find a different way to select the element.所以你需要找到一种不同的方式来 select 元素。 Since it is a sibling, you can use prev() to select it.由于它是兄弟,您可以使用 prev() 到 select 它。

 $(document).ready(function() { $('span').hover(function() { $(this).addClass('underline').prev("img").show(); }, function() { $(this).removeClass('underline').prev("img").hide(); }); });
 .hidden { display: none; height: 100px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <table> <tr> <th>Best Albums</th> <th>Year</th> </tr> <tr> <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/2Lm375yVwwGyAHfNFP2HU6-970-80.jpg.webp" /> <span>Spreading the disease</span></td> <td>1985</td> </tr> <tr> <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/jP4Fx9doBk4R27kg5hpsrd-970-80.jpg.webp" /> <span>Among the living</span></td> <td>1987</td> </tr> <tr> <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/sj49Yj3eJERgwwVbZBZ5nJ-970-80.jpg.webp" /> <span>Persistence of time</span></td> <td>1990</td> </tr> <tr> <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/BhDq2Bdm7axUP9erRcs5i9-970-80.jpg.webp" /> <span>Sound of white noise</span></td> <td>1993</td> </tr> <tr> <td><img class="hidden" id='image' src="https://cdn.mos.cms.futurecdn.net/fKVNiGMLbFtsXt6uC8aLMm-970-80.jpg.webp" /> <span>We've come for you all</span></td> <td>2003</td> </tr> </table> </div> <br><a href="scott.html"><button type="button">Check them out</button></a><br> </body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将鼠标悬停在缩略图上时,如何为其他视频进行视频交换? - How do I make a video swap for a different video when I hover over a thumbnail? 当其他元素悬停在元素上时,如何更改:hover状态? - How do I change :hover status on an element when a different element is hovered over? 如何在激活 CSS ":hover" 的纯 JavaScript 中模拟鼠标悬停? - How do I simulate a mouseover in pure JavaScript that activates the CSS ":hover"? 我怎样才能让 javascript 检测不同的显示尺寸,然后根据尺寸转到网站? - How can I make it so javascript detects different display sizes and then goes to a website depending on the size? 当我将鼠标悬停在文本上时,它会改变颜色并且颜色保持不变,我该如何做到这一点? - How do I make it so when I hover over my text it changes color and the color stays changed? 如何根据不同的设备使用鼠标悬停和单击? - How can I use hover and click depending on different devices? 如何在不同设备上连续显示固定数量的图像 - How can i display fixed number of images in a row on different devices 将鼠标悬停在卡片上时,我可以更改卡片的图像。 但是对于大量具有不同图像的卡片,我该怎么做 - I'm able to change the image of a card when it is being hovered over. But how do I do this for a large number of cards with different images 如何使用CSS或Javascript在鼠标悬停时尽可能简单,轻便地切换图像? - How can I use CSS or Javascript to switch images on mouseover as simple and lightweight as possible? 当我将鼠标悬停在wordpress循环中的每个帖子上时,会执行不同的操作 - different actions when I hover over each post in the wordpress loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM