首页 > 解决方案 > 如何在javascript中的链接中获取元素?

问题描述

我想制作一个模拟器,告诉您该网站上特定用户名的个人资料的最后评论。我想从该链接(链接为“ https://scratch.mit.edu/users/oreyelephant/ ”)中定位特定的类或ID ,并检测最新评论的简单文本,并将其放入一个div或段落标签。对于这个声明,我想在控制台中声明关于我的文本。这是我所拥有的:

var link = "https://scratch.mit.edu/users/oreyelephant/"
var aboutMe = link.getAttribute("editable read");
console.log(aboutMe);

我拥有“可编辑读取”的 getAttribute 的原因是因为这是我要获取其文本的类的名称。我试图获取 text/innerText 的地方

标签: javascripthtmlhyperlinkelement

解决方案


要了解用户,请不要直接尝试这样做,请使用临时 API。

var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('GET', 'https://api.scratch.mit.edu/users/oreyelephant', true);
    xmlhttp.send();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            var response = JSON.parse(xmlhttp.responseText);
            console.log(response["profile"]["bio"])
        }
    };


推荐阅读