Toggle image on hover of thumbnails?

心不动则不痛 提交于 2019-12-12 04:13:45

问题


please see this fiddle: http://jsfiddle.net/rabelais/e47xq/2/

I want to show the large image of the thumbnail when the uses hovers on the thumbnail. Below is my starting point. Any suggestions how to do this? In my final version there will be possible 20 thumbnails so I need the code to be as concise as possible.

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

回答1:


.attr()

$(this).attr('href') get current a tag href attribute.

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

and

set 2nd image id to image-2

Fiddle Demo

Id must be unique. Don't use same id for multiple elements.

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




回答2:


Well, probably not the best overall solution, but given what you have, check this fiddle: http://jsfiddle.net/e47xq/1/

Use $(this).attr('href') on the anchor element to retrieve the selector you need.



来源:https://stackoverflow.com/questions/23494169/toggle-image-on-hover-of-thumbnails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!