我有两个图像,我需要在单击图像时进行切换。
<img id='arrowRotate' src='images/prof_arrow1.png' data-swap='images/prof_arrow.png'
data-src='images/prof_arrow1.png' />
当用户单击图像时,src
应获取 data-swap
的值,当您再次单击时,src
应更改为数据源
。这应该像切换一样持续发生。任何帮助表示赞赏。
$("#arrowRotate").click(function() {
var swapImage = $("#arrowGrey").attr("data-swap");
$("#arrowGrey").attr({
'src': swapImage,
id: 'arrowOrange'
});
});
这就是我到目前为止所取得的成果。
最佳答案
根据我对您问题的理解,希望这是您所期望的,
$("#arrowRotate").click(function() {
var _this = $(this);
var current = _this.attr("src");
var swap = _this.attr("data-swap");
_this.attr('src', swap).attr("data-swap",current);
});
<强> DEMO Fiddle
关于javascript - 使用 jQuery 在单击时切换图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27271855/