简体   繁体   中英

HTML DOM and Javascript: Uncaught TypeError: Cannot read property 'appendChild' of undefined

I have written a function that scrapes a website's most popular articles. I perform two scrapes, one for the article image and the other for the link and title of the article. I then merge the image, link, and title for each article into separate objects. From here, my goal is to write this information to an html document. I create a table row for each article. Each row has three td's: one for title, image, and link. The error I receive is shown below. I have put ** around the place where the error appears. It occurs when I try to append my table row to my table. Note: This is for a small project. I am not scraping a website for any personal benefit.

VM51039:35 Uncaught TypeError: Cannot read property 'appendChild' of undefined
at <anonymous>:35:46
at Array.forEach (<anonymous>)
at m_getArticles (<anonymous>:12:15)
at <anonymous>:1:1

 var m_getArticles = function(){
      var article_nameANDlink = d3.selectAll('.grid.most-popular__grid h3>a').nodes();
      var article_img = d3.selectAll('.grid.most-popular__grid picture>img').nodes();

      var combination = [];
      for (i=0; i<article_nameANDlink.length; i++) {
        var info = new Object();
        info.nameANDlink=article_nameANDlink[i];
        info.img=article_img[i]; 
        combination.push(info); 
      }
      combination.forEach(function(d, index, arr) {
        var link = d.nameANDlink.getAttribute("href");
        console.log(link);
        var name = d.nameANDlink.innerText;
        console.log(name);
        var img = d.img.getAttribute("src");
        console.log(img);



        var header = document.createElement("p");
        var title = document.createTextNode(name);
        header.appendChild(title);

        var present = document.createElement("img");
        present.setAttribute("img",img);

        var provide = document.createElement("a");
        provide.setAttribute("a",link);
        provide.innerText="Read Article";

        var nth_article = document.createElement("TR");
        nth_article.setAttribute("id","row"+index);
        document.getElementsByTagName("table")[0].**appendChild(nth_article)**;

        var within_info = document.createElement("td");
        within_info.setAttribute("id","within_info"+index);
        document.getElementById("row"+index).appendChild(header);

        var within_img = document.createElement("td");
        within_img.setAttribute("id","within_img"+index);
        document.getElementById("row"+index).appendChild(present);

        var within_img = document.createElement("td");
        within_img.setAttribute("id","within_link"+index);
        document.getElementById("row"+index).appendChild(provide);


      })

    }

If your table element already exist try to pass with var

 var mytable = document.getElementsByTagName("table")[0];
    mytable.appendChild(nth_article);

the best way it's using js for create your table same other element

将获得的表元素存储在变量中,并检查其是否未定义(如果未定义),然后创建表元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM