【问题标题】:jQuery Image Swap for Hovering over Div's用于悬停在 Div 上的 jQuery 图像交换
【发布时间】:2009-04-20 17:14:16
【问题描述】:
$(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();

【问题讨论】:

    标签: jquery image swap


    【解决方案1】:

    如果您仍想使用哈希,您可以使用此代码获取它(假设 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();
            }
        );
    });
    

    【讨论】:

    • 我想你可能打算放一个“#”而不是“。”
    猜你喜欢
    • 1970-01-01
    • 2011-11-13
    • 2015-10-03
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多