gpt4 book ai didi

javascript - 将链接列表从数组保存到文本文件(几乎可以正常工作)

转载 作者:行者123 更新时间:2023-11-28 18:33:38 26 4
gpt4 key购买 nike

我试图将所有链接字符串保存到文本文档中,但它只保存文档中的最后一个链接(在本例中为 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();

有人可以帮我吗?谢谢你! :-)

最佳答案

(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();

})();

https://jsfiddle.net/um4qhsks/1/

关于javascript - 将链接列表从数组保存到文本文件(几乎可以正常工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37510238/

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