Javascript 未使用Jquery在单击事件时开始下载
这是我的代码:Javascript 未使用Jquery在单击事件时开始下载,javascript,jquery,Javascript,Jquery,这是我的代码: var _t = $(this); _t.parents('td').find('.save-file').attr("href",_OBJECT_URL); _t.parents('td').find('.save-file').attr('download', fileName); _t.parents('td').find('.save-file').css('display','block'); _t.parents('td').find('.save
var _t = $(this);
_t.parents('td').find('.save-file').attr("href",_OBJECT_URL);
_t.parents('td').find('.save-file').attr('download', fileName);
_t.parents('td').find('.save-file').css('display','block');
_t.parents('td').find('.save-file').click();
\u OBJECT\u URL
包含如下链接blob:https://www.example.com/53713783-ba94-4d5d-9221-54aa1fd87738 CNN:14032:12 2019-07-22_12h32m44s.jpg
并且fileName
包含一个名称行,该2019-07-22_12h32m44s.jpg
我在同一页上点击了多个“.save file”链接。我还做了这个函数来调试
$(document).on('click','.save-file', function(event){
console.log("CLICKED");
console.log($(this).href);
console.log($(this).download);
});
当我使用jquery单击.save文件时,就会打印出来
点击
未定义
未定义
如果我手动单击.save file,则开始下载。您需要使用
attr()
使用jQuery访问元素属性
$(文档)。在('click','save file',函数(事件){
控制台日志(“单击”);
console.log($(this.attr('href'));
log($(this.attr('download'));
});代码>
单击我
jQuery对象没有href或download-properties,用于检索值:
$(document).on('click','.save-file', function(event){
console.log("CLICKED");
console.log($(this).attr("href"));
console.log($(this).attr("download"));
});
使用下一个
_t.parents('td').find('.save-file').click(function() {
window.location = _OBJECT_URL;
});
它将建议用户从url:\u OBJECT\u url下载您的文件
您将不需要下一个代码:
_t.parents('td').find('.save-file').attr("href",_OBJECT_URL);
_t.parents('td').find('.save-file').attr('download', fileName);
_t.parents('td').find('.save-file').css('display','block');
如果您想设置属性href
,下载
等:
_t.parents('td').find('.save-file').click(function() {
window.location = $(this).attr('download'); // or $(this).attr('href')
});
在jquery中,您不能像以前那样访问下载和href属性。您需要使用.attr()方法来访问这些属性
$(document).on('click', '.save-file', function(event) {
console.log("CLICKED");
console.log($(this).attr('href'));
console.log($(this).attr('download'));
});
您是否尝试触发(“单击”)而不是.click();同时显示html代码我不直接使用指针。我用这个$(_t).parents('td').find('save file').attr('href','u OBJECT\u URL);