【问题标题】:Toggle image on hover of thumbnails?在缩略图悬停时切换图像?
【发布时间】:2014-05-06 12:01:35
【问题描述】:

请看这个小提琴:http://jsfiddle.net/rabelais/e47xq/2/

当用户悬停在缩略图上时,我想显示缩略图的大图像。下面是我的出发点。任何建议如何做到这一点?在我的最终版本中,可能会有 20 个缩略图,所以我需要代码尽可能简洁。

$(".thumnails").on("mouseover mouseout", "a", function () {
$('#').toggle();
});

【问题讨论】:

    标签: jquery image toggle show


    【解决方案1】:

    .attr()

    $(this).attr('href') 获取当前标签href 属性。

    $(".thumnails").on("mouseover mouseout", "a", function () {
        $('#' + $(this).attr('href')).toggle();
    });
    

    将第二张图片ID设置为image-2

    Fiddle Demo

    ID 必须是唯一的。不要对多个元素使用相同的 id。

    阅读Two HTML elements with same id attribute: How bad is it really?

    【讨论】:

    • 感谢您注意到我的错误,我知道只能使用唯一 ID,这是一个复制错误。
    • @angela 你想做title 属性 --> jsfiddle.net/cse_tushar/e47xq/3
    【解决方案2】:

    好吧,可能不是最好的整体解决方案,但鉴于你所拥有的,请检查这个小提琴:http://jsfiddle.net/e47xq/1/

    在锚元素上使用$(this).attr('href') 来检索您需要的选择器。

    【讨论】:

    • 谢谢。如果我想将缩略图链接到大图的标题属性而不是 id 怎么办?
    猜你喜欢
    • 2011-05-11
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2014-01-15
    相关资源
    最近更新 更多