function clickLink(link) {
var cancelled = false;
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0,
false, false, false, false,
0, null);
cancelled = !link.dispatchEvent(event);
}
else if (link.fireEvent) {
cancelled = !link.fireEvent("onclick");
}
if (!cancelled) {
window.location = link.href;
}
}
for (var i = 0; i < links.length; i++) { var x=links[i]; if(x.href.indexOf('download.mp4') > -1) { console.log(x.href); x.click(); }}
for (var i = 0; i < links.length; i++) { var x=links[i]; if(x.href.indexOf('download.mp4') > -1) { console.log(x.href); clickLink(x); }}
使用上述代码,目的是在一个页面上下载所有视频链接。虽然控制台日志包含我想要的所有链接,但实际上只下载了一个链接。 没有错误或警告,我怎样才能做到这一点? 也试过没有window.location线,没有运气......
现在解决了:
function JustclickLink(link) {
var cancelled = false;
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0,
false, false, false, false,
0, null);
cancelled = !link.dispatchEvent(event);
}
else if (link.fireEvent) {
cancelled = !link.fireEvent("onclick");
}
if (!cancelled) {
window.open(link.href,'_blank');
}
} for(var i = 0; i&lt; links.length; i ++){var x = links [i]; if(x.href.indexOf('download.mp4')&gt; -1){console.log(x.href); JustclickLink(X); }}
答案 0 :(得分:0)
这是你所寻求的jquery替代品。 Window.open应该用于多次下载,因为window.location只会在代码运行中触发一次。
$('#foo').on('click', function() {
window.open($(this).attr("href"),'_blank');
});
$('#foo').trigger('click');