最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - An error occur when I use appendChild ("<br>") in a genuine exist reference - Stack

programmeradmin4浏览0评论

This is a beginner's question...

I want to write some code to initialize a bunch of seats picture in my web page,

create and set attribute to them, and make them change line every 9 seats (4 rows, and for every row it has 9 seats), here is the code

function initSeats() {

            var seatsDiv = document.getElementById("seats");

            //Initialize the appearence of all seats
            for (var i = 0; i < seats.length; i++) {
                for (var j = 0; j < seats[i].length; j++) {
                    var currentSeatIndex = i * seats[i].length + j;
                    if (seats[i][j]) {
                        //if current seat is available(true), create a new IMG element and set some attributes;
                    } else {
                        //if current seat is unavailable(true), create a new IMG element and set some attributes;
                    }
                }
                seatsDiv.appendChild("<br>");//here is the problem
            }
        }

what I want to do is when one of the outer loop finished, add a
at the end,

But then I got a "NotFoundError" in Chrome that looks like the node seatsDiv doesn't exist

so after all only one row of seats were successfully initialized.

Is appendChild supposed to append anything at the position? Or should I use some other method?

This is a beginner's question...

I want to write some code to initialize a bunch of seats picture in my web page,

create and set attribute to them, and make them change line every 9 seats (4 rows, and for every row it has 9 seats), here is the code

function initSeats() {

            var seatsDiv = document.getElementById("seats");

            //Initialize the appearence of all seats
            for (var i = 0; i < seats.length; i++) {
                for (var j = 0; j < seats[i].length; j++) {
                    var currentSeatIndex = i * seats[i].length + j;
                    if (seats[i][j]) {
                        //if current seat is available(true), create a new IMG element and set some attributes;
                    } else {
                        //if current seat is unavailable(true), create a new IMG element and set some attributes;
                    }
                }
                seatsDiv.appendChild("<br>");//here is the problem
            }
        }

what I want to do is when one of the outer loop finished, add a
at the end,

But then I got a "NotFoundError" in Chrome that looks like the node seatsDiv doesn't exist

so after all only one row of seats were successfully initialized.

Is appendChild supposed to append anything at the position? Or should I use some other method?

Share Improve this question asked Sep 30, 2013 at 7:59 Crazy MulberryCrazy Mulberry 1271 gold badge2 silver badges8 bronze badges 2
  • 2 appendChild should be given a node, not a string. – Virus721 Commented Sep 30, 2013 at 8:01
  • @Virus721 thanks a lot, looks like I'm not thinking in the right way – Crazy Mulberry Commented Sep 30, 2013 at 8:06
Add a ment  | 

1 Answer 1

Reset to default 15

appendChild expects an HTMLElement rather than a string, try switching to:

seatsDiv.appendChild(document.createElement("br"));
发布评论

评论列表(0)

  1. 暂无评论