如何返回javascript DOM元素对象,并从调用者appendChild(obj)它

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

本文关键字:调用者 appendChild obj 元素 何返回 返回 javascript DOM 对象      更新时间:2023-09-26

我有一个javascript函数drawGrid()创建一个表,行和单元格。如果在函数中添加appendChild(table),则一切正常,表将被显示。

但是我想返回表格并在调用drawGrid()

的函数中进行追加操作

这里是drawGrid()的结束:

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

调用drawGrid():

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

未捕获类型错误:在'Node'上执行'appendChild'失败:参数1不是'Node'类型。

我怎么能返回表,所以它到达时仍然记得它是一个实例的节点和HTMLElement,所以我可以appendChild(表)?



在整个代码下面:

  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;
      }
  };

和其他地方:

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);
误差

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