繁体   English   中英

用于悬停在 Div 上的 jQuery 图像交换

[英]jQuery Image Swap for Hovering over Div's

$(document).ready(function () {
    // Hide all large images except the first one
    $('#imageContainer img').hide().filter(':first').show();
    // Select all thumb links
    $('#thumbContainer a').hover(function (event) {
        // Hide all large images except for the one with the same hash as our thumb link
        $('#imageContainer img').hide().filter(this.hash).show();
    },
        function () { } // Because the hover method has a mouseout state we need to define too
    );
});

这个 .js 脚本适用于锚标记上的鼠标悬停。 但是,我希望这个函数可以在整个 div 上工作。

如何更改这部分: .filter(this.hash).show();

到这个: .filter(this.(id or unique name).show();

如果您仍然想使用散列,您可以使用以下代码获取它(假设this是您的 div):

var hash = $(this).find('a').get(0).hash;

如果你想使用关于 div 的一些独特的东西,我之前使用过的 div id 等于 img 的 className。

如果你有这个 html:

<div id="container1" class="thumbContainer"></div>
<div id="imageContainer">
  <img src="" alt="" class="container1" />
</div>

你可以使用这样的东西,(我把你的悬停改为鼠标悬停,因为你只使用了它):

$(document).ready(function(){
    // Hide all large images except the first one
    $('#imageContainer img').hide().filter(':first').show();
    // Select all thumb links
    $('.thumbContainer').mouseover(function(event) {
            // Hide all large images except for the one with the same hash as our thumb link
            $('#imageContainer img').hide().filter("." + this.id).show();
        }
    );
});

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM