Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我们可以返回document.createElement(“元素”)吗?_Javascript - Fatal编程技术网

Javascript 我们可以返回document.createElement(“元素”)吗?

Javascript 我们可以返回document.createElement(“元素”)吗?,javascript,Javascript,我必须多次创建一个输入框,所以我多次调用这样的函数: function __createInputBox(id) { var input = document.createElement("input"); input.setAttribute('id', id); input.setAttribute('type', 'number'); input.setAttribute('name', id); input.setAttribute('value

我必须多次创建一个输入框,所以我多次调用这样的函数:

function __createInputBox(id) {
    var input = document.createElement("input");

    input.setAttribute('id', id);
    input.setAttribute('type', 'number');
    input.setAttribute('name', id);
    input.setAttribute('value', '0');
    input.setAttribute('min', '0');

    return input;
}
在我的主函数中,我将其附加为:

var box = __createInputBox(id);
element.appendChild(box);
我不断地发现这个错误:

__createInputBox is not defined
那么允许我们从document.createElement(“元素”)返回值吗?如果这样做不好,那么向页面添加多个元素的更好方法是什么

以下是我声明函数的方式:

function InputSpace(){
this.inputSpace = document.getElementById("inputspace");
this.num = 1;

    function __createInputBox(id) {
       // function  declared here
    }
这就是我所说的代码:

InputSpace.prototype = {
constructor: InputSpace,
drawInputSpace : function () {
  var i = 0,
      max;
  var table = document.createElement("TABLE");
  var table_body = document.createElement("TBODY");
  for(max = num; i<num; i++){
        var element = document.createElement("TR");
        var box = __createInputBox(id);
        element.appendChild(box);
        table_body.appendChild(element);
  }
  table.appendChild(table_body);
  this.inputSpace.appendChild(table);
}
InputSpace.prototype={
构造函数:InputSpace,
drawInputSpace:函数(){
var i=0,
最大值;
var table=document.createElement(“表”);
var table_body=document.createElement(“TBODY”);

对于(max=num;i在阅读注释后,我们可以从函数返回
document.createElement(“元素”)
in。函数未定义的原因是
错误,因为我调用的函数超出了它的作用域。下面显示了调用对象私有函数的正确方法

更改代码以正确访问私有函数:

function InputSpace(){
    // public attributes
    this.inputSpace = document.getElementById("inputspace");
    this.num = 1;
}

InputSpace.prototype = (function(){
     // private functions
     var __createInputBox(id){
     var input = document.createElement("input");

         input.setAttribute('id', id);
         input.setAttribute('type', 'number');
         input.setAttribute('name', id);
         input.setAttribute('value', '0');
         input.setAttribute('min', '0');

         return input;
     }; 
     return {
     constructor: InputSpace,
     // public functions
     drawInputSpace : function () {
         var i = 0,
         max;
         var table = document.createElement("TABLE");
         var table_body = document.createElement("TBODY");
         for(max = num; i<num; i++){
             var element = document.createElement("TR");
             var box = __createInputBox(id);
             element.appendChild(box);
             table_body.appendChild(element);
         }
         table.appendChild(table_body);
         this.inputSpace.appendChild(table);
    }
})();
函数InputSpace(){
//公共属性
this.inputSpace=document.getElementById(“inputSpace”);
this.num=1;
}
InputSpace.prototype=(函数(){
//私人职能
变量\uuuCreateInputBox(id){
var输入=document.createElement(“输入”);
input.setAttribute('id',id);
input.setAttribute('type','number');
input.setAttribute('name',id);
input.setAttribute('value','0');
input.setAttribute('min','0');
返回输入;
}; 
返回{
构造函数:InputSpace,
//公共职能
drawInputSpace:函数(){
var i=0,
最大值;
var table=document.createElement(“表”);
var table_body=document.createElement(“TBODY”);

对于(max=num;i显示代码,这可能是一个范围问题,这意味着对于调用
\uuu createInputBox
的代码来说,没有这样的函数,您的问题是无法找到函数
\uu createInputBox
!它与该函数内部发生的任何事情无关。很可能您正在定义函数在另一个作用域中的上…\uu createInputBox未定义它与返回无关…尝试在page loadWorks@user3806549之后调用它。我正在动态创建元素。因此
document.getElementById
不会返回任何内容