简体   繁体   中英

How to return javascript DOM Element Object, and appendChild(obj) it from caller?

I have a javascript function drawGrid() that creates a table, rows and cells. If I appendChild(table) inside the function, all is well, the table gets displayed.

But I want to return the table and do the appending in the function that calls drawGrid()

Here the end of drawGrid():

console.log(table instanceof HTMLElement) // true
console.log(table instanceof Node)        // true
return table;

Where I call drawGrid():

var table = new Grid()....
console.log(table instanceof HTMLElement) // false
console.log(table instanceof Node)        // false
......appendChild(table);

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

How can I return table, so that it arrives still remembering that it is an instanceof Node and HTMLElement, so I can appendChild(table)?



Below the whole code:

  function Grid(rows, cols, cellSize, line, thicker, xDivision, yDivision, gridColor, topMargin, opacity) {
    Shape.apply(this, arguments);
    this.rows = rows;
    this.cols = cols;
    this.cellSize = cellSize;
    this.line = line;
    this.thicker = thicker;
    this.xDivision = xDivision;
    this.xAccent = rows/xDivision;
    this.yDivision = yDivision;
    this.yAccent = cols/yDivision;
    this.topMargin = topMargin;
    this.opacity = opacity;
    this.gridColor = gridColor;
    this.drawGrid(this.rows, this.cols, this.cellSize, this.line, this.thicker, this.xAccent, this.yAccent, this.gridColor, this.topMargin, this.opacity);
  };
  Grid.prototype = Object.create(Shape.prototype);
  Grid.prototype.constructor = Grid;

  Grid.prototype = {
      drawGrid: function(rows, cols, cellSize, line, thicker, xAccent, yAccent, gridColor, topMargin, opacity) {
          var table = document.createElement("TABLE");
          table.setAttribute("id", "hundred_square");
          table.style.cssText +=';'+ "width: "+ (cols*cellSize) +"vw; height: "+ (rows*cellSize) +"vw; border-collapse: collapse; border: "+ (5*line) +"vw solid" +gridColor+ "; margin: " +topMargin+ "vw auto;";
          for(var i=0; i < rows; i++){
              var row = document.createElement("TR");
              table.appendChild(row);
              row.style.cssText += ';' + "opacity: "+ opacity +"; filter: alpha(opacity="+ (opacity*100) +");";
              for(var j=0; j < cols; j++){
                var cell = document.createElement("TD");
                row.appendChild(cell);
                cell.style.cssText += ';' + "width: "+ cellSize +"vw; height: "+ cellSize +"vw; border-right: "+ (j%yAccent==(yAccent-1) ? thicker*line : line) +"vw solid " +gridColor+ "; border-bottom: "+ (i%xAccent==(xAccent-1) ? thicker*line : line) +"vw solid " +gridColor+ ";opacity: 0."+ opacity +"; filter: alpha(opacity="+ (opacity*100) +");";
              }
          }
          //document.getElementById("js_animation").appendChild(table);
          console.log(table instanceof HTMLElement)   //true
          console.log(table instanceof Node)          //true
          return table;
      }
  };

and somewhere else:

var table = new sl.Shapes.Grid(10, 10, 3.5, 0.1, 5, 2, 2, "#bfbf8c", 4.5, 0.85);
console.log(table instanceof sl.Shapes.Grid) // true
console.log(table instanceof HTMLElement)    // false
console.log(table instanceof Node)           // false
document.getElementById("js_animation").appendChild(table);

Error

看起来tableGrid ,而不是HTML表本身,应该使用(new Grid()).drawGrid()来获取表,然后可以将其追加到HTML元素。

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