简体   繁体   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
    );
});

This .js script works for a mouseover on an anchor tag.这个 .js 脚本适用于锚标记上的鼠标悬停。 However, I would like this function to work on an entire div.但是,我希望这个函数可以在整个 div 上工作。

How do I change this part : .filter(this.hash).show();如何更改这部分: .filter(this.hash).show();

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

If you still want to use the hash you could get it using this code (assuming that this is your div):如果您仍然想使用散列,您可以使用以下代码获取它(假设this是您的 div):

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

If you want to use something unique about the div I've used the id of the div equal to the className of the img before.如果你想使用关于 div 的一些独特的东西,我之前使用过的 div id 等于 img 的 className。

If you had this html:如果你有这个 html:

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

You could use something like this, (I changed your hover to a mouseover, since you were only using that):你可以使用这样的东西,(我把你的悬停改为鼠标悬停,因为你只使用了它):

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