【发布时间】:2015-03-11 10:11:24
【问题描述】:
我创建了一个 html 页面,它显示图像并根据左右箭头键导航到其他图像。
有一个带有 download 属性的锚标记。当前显示在屏幕上的图片路径通过jquery设置为该标签的href。
单击此链接可下载图像。我需要按下向下箭头时,应该下载图像。 (总之应该触发这个锚标签的点击事件。)
我尝试了 jquery trigger 函数但无法正常工作。下面是我的代码。
提前致谢。
<!doctype HTML>
<html>
<head>
<title>My Page</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style type="text/css">
div.links {
top: 95%;
left: 45%;
position: fixed;
}
a.dLink, a.nav {
text-decoration: none;
}
label {
background: #330033;
padding: 2px;
color: #CC9933;
cursor: pointer;
}
</style>
</head>
<body>
<input type="hidden" id="iNames"
value="IMG1.jpg,IMG2.jpg,IMG3.jpg">
<img class="pic" />
<div class="links">
<label class="prev">Prev</label>
<a class="dLink" href="" download>
<label>Download</label>
</a>
<label class="next">Next</label>
</div>
<script type="text/javascript">
$(document).ready(
function() {
iNames = ($("#iNames").val()).split(",");
total = iNames.length;
i = 0;
$("img.pic").attr("src",iNames[i]);
$("a.dLink").attr("href", $("img.pic").attr("src"));
$(document).keydown(function(e) {
switch (e.which) {
case 37: // left
prev();
break;
case 39: // right
next();
break;
case 40: // down
$("a#dLink").trigger("click"); //this is not working
break;
default:
return;
}
});
function next() {
i = i == total - 1 ? 0 : i + 1;
setImage();
}
function prev() {
i = i == 0 ? total - 1 : i - 1;
setImage();
}
function setImage() {
$("img.pic").attr("src",iNames[i]);
$("a.dLink").attr("href", $("img.pic").attr("src"));
}
$("label.next").click(function() {
next();
});
$("label.prev").click(function() {
prev();
});
});
</script>
</body>
</html>
【问题讨论】:
标签: jquery html download anchor