gpt4 book ai didi

javascript - .click() 在修改其下载属性后未通过 anchor 标记触发

转载 作者:搜寻专家 更新时间:2023-10-31 21:49:47 26 4
gpt4 key购买 nike

我的情况是“系统需要询问用户‘你是要打开图像还是下载图像。?’通过使用确认框。如果用户按下“确定”,我们不应该阻止该 anchor 标记的默认操作,随它去吧,但如果用户按下“取消”,则应该下载特定图像...

HTML:

<a href="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=3c6263c3453b">test</a>

JS:

$('a').click(function (e) {
if (!$(this).is('[download]')) {
var cond = confirm('Press ok to view, cancel to save..');
if (!cond) {
e.preventDefault();
$(this).attr('download', 'download').click().removeAttr('download');
}
}
});

DEMO

任何人都可以告诉如何实现这个..?

最佳答案

您需要使用 this.click(); 作为 HTMLElement.click()方法模拟鼠标点击元素。

$(this).click(); 只会触发 jquery 点击处理程序和 onclick 处理程序绑定(bind)到元素没有别的。

$('a').click(function (e) {
if (!$(this).is('[download]')) {
var cond = confirm('Press ok to view, cancel to save..');
if (!cond) {
e.preventDefault();
$(this).attr('download', 'download');
this.click()
$(this).removeAttr('download');
}
}
});

DEMO

关于javascript - .click() 在修改其下载属性后未通过 anchor 标记触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24571041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com