我试图将所有链接字符串保存到文本文档中,但它只保存文档中的最后一个链接(在本例中为Youtube.com)。
我希望它保存所保存的txt文档的所有链接,我做错了什么?
https://jsfiddle.net/zfL2hzvp/4/
var links = document.querySelectorAll('a');
// Loop through all links
for (var i = 0; i < links.length; i++) {
// Store links in variable
var linksArray = links[i];
// Works fine in console
console.log(linksArray);
}
// Create text document — only saves 1st link in text doc
var textDoc = document.createElement('a');
textDoc.href = 'data:attachment/text,' + encodeURI(linksArray);
textDoc.target = '_blank';
textDoc.download = 'myFile.txt';
textDoc.click();
有人可以帮助我吗? 谢谢! : - )
答案 0 :(得分:3)
(function() {
var links = document.querySelectorAll('a');
var linksArray = [];
// Loop through all links
for (var i = 0; i < links.length; i++) {
// Store links in variable
linksArray.push(links[i]);
// Works fine in console
console.log(linksArray);
}
// Create text document — only saves 1st link in text doc
var textDoc = document.createElement('a');
textDoc.href = 'data:attachment/text,' + encodeURI(linksArray.join('\n'));
textDoc.target = '_blank';
textDoc.download = 'myFile.txt';
textDoc.click();
})();