我在github上有这个html文件,我想使用JavaScript访问它。我尝试过这样做,但不起作用:(我使用的是p5.js,所以setup函数基本上就是onload函数)
var htmlfile = "[URL THAT POINTS TO HTML FILE]";
function setup() {
console.log(htmlfile.getElementById('id'))
}
有没有办法做到这一点?最好,我希望只使用普通的JavaScript和p5。
发布于 2018-08-01 23:36:15
正如评论中所说,向原始的github页面发送请求可能是获取您想要的html的最佳方式。下面是一个使用fetch的示例
document.addEventListener('DOMContentLoaded', getHTML);
function getHTML() {
fetch('https://gist.githubusercontent.com/chrisvfritz/bc010e6ed25b802da7eb/raw/18eaa48addae7e3021f6bcea03b7a6557e3f0132/index.html')
.then((res) => {
return res.text();
})
.then((data) => {
document.write(data);
// get the p tag from the remote html file now we have the html in our document
var firstParagraph = document.getElementsByTagName("p")[0];
console.log(firstParagraph.textContent);
})
}
https://stackoverflow.com/questions/51634495
复制相似问题