Javascript js中的create属性触发异常';不是一个函数';

Javascript js中的create属性触发异常';不是一个函数';,javascript,html,firefox,scratchpad,Javascript,Html,Firefox,Scratchpad,我正在使用js来更改一个div的内容,该div包含我想要在Ajax中使用的内容输入, 我使用Firefox scratchpad调试此函数: function show(id){ var div = document.getElementById('com'); div.innerHTML = ''; var input1 = document.createElement('input') .createAttribute('type').setAttribute('ty

我正在使用js来更改一个div的内容,该div包含我想要在Ajax中使用的内容输入, 我使用Firefox scratchpad调试此函数:

function show(id){
var div = document.getElementById('com');
div.innerHTML = '';
var input1 = document.createElement('input')
            .createAttribute('type').setAttribute('type', 'hidden')
            .createAttribute('value').setAttribute('value',id )
            .setAttribute('name','id' );
var input2 = document.createElement('input')
             .createAttribute('type').setAttribute('type', 'input')
             .createAttribute('name').setAttribute('name', 'com');
var btn = document.createElement('button')
          .createAttribute('onClick').setAttribute('onClick', 'post()');
btn.innerHTML = 'Comment';
div.appendChild(input1).appendChild(input2).appendChild(btn);
}
我得到的是:

/*
Exception: document.createElement(...).createAttribute is not a function
@Scratchpad/2:2
*/

我什么都不懂,有什么想法吗?

我相信
.createAttribute()
属于
文档
,而不是单个元素,所以这可以解释错误:
.createElement()
返回一个元素,而该元素没有函数
.createAttribute()

但是在调用
.setAttribute()
之前不需要使用
.createAttribute()
,因为后者将创建元素属性(如果它们不存在)。但是,我认为
.setAttribute()
返回
未定义的
,因此无法真正链接它。试着一步一步地做:

var input1 = document.createElement('input');
input1.setAttribute('type', 'hidden');
input1.setAttribute('value',id );
input1.setAttribute('name','id' );
// etc.

基本上,这个异常表示没有名为“createAttribute”的函数。这是正确的:

.createAttribute()
文档
的一个函数:

因此,函数不能像您尝试的那样被链接。你必须单独给他们打电话。无论如何,“createAttribute”不应该再使用了(请参阅)

功能显示(id){
var div=document.getElementById('com');
div.innerHTML='';
var input1=document.createElement('input');
input1.setAttribute('type','hidden');
input1.setAttribute('value',id);
setAttribute('name','id');
var input2=document.createElement('input');
setAttribute('type','input');
input2.setAttribute('name','com');
var btn=document.createElement('button');
setAttribute('onClick','post()');
btn.innerHTML='Comment';
div.appendChild(input1).appendChild(input2).appendChild(btn);
}