首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用JavaScript访问github html文件

使用JavaScript访问github html文件
EN

Stack Overflow用户
提问于 2018-08-01 21:24:43
回答 1查看 40关注 0票数 0

我在github上有这个html文件,我想使用JavaScript访问它。我尝试过这样做,但不起作用:(我使用的是p5.js,所以setup函数基本上就是onload函数)

代码语言:javascript
复制
var htmlfile = "[URL THAT POINTS TO HTML FILE]";

function setup() {
    console.log(htmlfile.getElementById('id'))
}

有没有办法做到这一点?最好,我希望只使用普通的JavaScript和p5。

EN

回答 1

Stack Overflow用户

发布于 2018-08-01 23:36:15

正如评论中所说,向原始的github页面发送请求可能是获取您想要的html的最佳方式。下面是一个使用fetch的示例

代码语言:javascript
复制
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);
    })
}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51634495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档