【问题标题】:How do I use mouseover (or hover) so that I can display different images when I go over different texts? JAVASCRIPT如何使用鼠标悬停(或悬停)以便在浏览不同文本时显示不同的图像? JAVASCRIPT
【发布时间】:2021-03-10 19:40:30
【问题描述】:

我目前正在做 CS50,我需要编写一个小 html 页面插入来自 Javascript 的交互式元素。我完全是这种语言的初学者,我正在尝试找出当我将鼠标悬停在上面时如何显示图像一段文字。 显然,互联网上关于这个的话题几乎为零,我只发现了这个,它确实有效,但只适用于第一张图片。原因是因为所有网址的“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>

【问题讨论】:

    标签: javascript html mouseover event-listener


    【解决方案1】:

    id 必须是唯一的。所以你需要找到一种不同的方式来选择元素。由于是兄弟,所以可以使用 prev() 来选择它。

    $(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>

    【讨论】:

    • 非常感谢。现在可以使用了!顺便说一句,这是 Jquery,对吗?它与“原始/基本”javascript 代码有很大不同吗?因为我想使用没有 Jquery 的更简单(至少对我而言)版本。
    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 2012-12-16
    • 2019-12-27
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 2018-11-16
    相关资源
    最近更新 更多