【问题标题】:Fetch elements from a website which load after 3 seconds从网站获取 3 秒后加载的元素
【发布时间】:2021-04-26 09:40:33
【问题描述】:

当我使用fetch 从网站获取数据时,我需要找到一种方法来获取在 3-4 秒后加载的元素。我应该如何尝试这样做?我的代码目前是

const body = await fetch('websiteurl')
let html = await body.text();

const parser = new DOMParser();
html = parser.parseFromString(html, 'text/html');

html.getElementById('pde-ff'); // undefined

我可以确保这个元素存在,如果我去网站并使用最后一行并将html 替换为document,它可以工作,但我需要等待网站加载。有什么想法吗?

【问题讨论】:

  • 在 DOM 上渲染 3 或 4 秒后加载的元素如何? http请求?脚本?
  • 呈现为 html 元素

标签: javascript html web dom


【解决方案1】:

你的东西几乎没有在正确的时间加载

所以你需要等待 html 打开并且有很多方法可以做到这一点

  1. 使用jquery $(function() {alert("It's loaded!");});
  2. 使用原版js window.addEventListener('load', function () {alert("It's loaded!");})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多