改变<;a>;通过javascript下载属性值
我正在尝试使用用户的输入更改改变<;a>;通过javascript下载属性值,javascript,jquery,html,web,download,Javascript,Jquery,Html,Web,Download,我正在尝试使用用户的输入更改标记下载属性值 用户应该能够输入文件名,然后单击下载。我找遍了,似乎找不到答案。你能给我一些建议吗 HTML: 您可以像这样使用jQuery: $('#filename').on('input', function() { document.getElementsByClassName("download")[0].download = document.getElementById("filename").value console.log("File
标记下载属性值
用户应该能够输入文件名,然后单击下载。我找遍了,似乎找不到答案。你能给我一些建议吗
HTML:
您可以像这样使用jQuery:
$('#filename').on('input', function() {
document.getElementsByClassName("download")[0].download = document.getElementById("filename").value
console.log("File name:" + document.getElementsByClassName("download")[0].download )
});
您可以在HTML中找到“查找小提琴”:
<input id="filename" type="text" onChange="inputChange(event)"/>
<a download="RENAME-ME.csv" href="#" class="download" id="button">
Download your file
</a>
href
属性是#
,因此在单击该链接时不会发生任何事情。现在还不清楚你想引起什么样的行为。这说明了如何做。
$('#filename').on('input', function() {
document.getElementsByClassName("download")[0].download = document.getElementById("filename").value
console.log("File name:" + document.getElementsByClassName("download")[0].download )
});
<input id="filename" type="text" onChange="inputChange(event)"/>
<a download="RENAME-ME.csv" href="#" class="download" id="button">
Download your file
</a>
function inputChange(event){
var filename = event.target.value;
var btn = document.getElementById('button');
btn.setAttribute('download', filename);
}