You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从API获取内容后,通过ID获取div返回null,求技术解决方案

问题分析与解决

你的代码存在核心错误:解析的HTML内容不正确,导致无法定位目标元素。

data.parse.text是一个对象,其中键名为*的属性才是API返回的页面HTML内容。你直接对整个对象执行JSON.stringify(),得到的是类似{"*": "<html>...</html>"}的字符串,用这个无效的字符串解析出的DOM结构并非目标页面的真实内容,自然找不到ig-tooltip元素。

修正后的代码

async function getSkillData(page) {
  try {
    const response = await fetch(`https://tarisland.wiki/wiki/api.php?action=parse&format=json&origin=*&page=${page}`);

    if (!response.ok) {
      throw new Error('Network response was not ok');
    }

    const data = await response.json();
    // 提取正确的HTML内容
    const htmlContent = data.parse.text['*'];
    const doc = new DOMParser().parseFromString(htmlContent, "text/html");

    console.log(doc.getElementById("ig-tooltip"));

    return data;
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}

getSkillData('Axe_Cyclone');

额外检查点

  • 可以先通过console.log(htmlContent)确认返回的HTML源码里确实包含id="ig-tooltip"的元素;
  • 若仍无法找到,需验证API返回的内容是否与预期页面一致。

内容的提问来源于stack exchange,提问作者M A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 05:57:41