为什么我的appendChild不会工作?

时间:2013-05-20 03:03:24

标签: javascript html html5 dom twitter-bootstrap

function purchase()
{
    var r=document.createElement('div');
    r.className="row";
    alert(r);
    var p=document.createElement('div');
    p.className="purchase-hero-unit";
    alert(p);
    var span1=document.createElement('div');
    span1.className="span3";
    span1.textContent="&nbsp";
    alert(span1);
    var span2=document.createElement('div');
    span2.className="span6";
    alert(span2);
    var span3=document.createElement('div');
    span3.className="span3";
    span3.textContent="&nbsp";
    alert(span3);
    var price=document.getElementById('pPrice').value;
    var name=document.getElementById('pName').value;
    var s=": $";
    var h=document.createElement('h3');
    h.textContent=price+s+name;
    alert(h);
    p.appendChild(h);
    span2.appendChild(p);
    r.appendChild(span1);
    r.appendChild(span2);
    r.appendChild(span3);
    var d=document.getElementById('spendingContainer');
    d.appendChild(r);
    alert(d);
}

这是我将两个文本框的输入附加到twitter bootstrap hero-unit的功能。 spendContainer基本上只是一个容器,任何 _ _-英雄单位基本上都是英雄单位。如果有人可以帮助解决它,将不胜感激。谢谢。

JSfiddle输出:

{"error": "Shell form does not validate{'html_initial_name': u'initial-js_lib', 'form': <mooshell.forms.ShellForm object at 0xa69168c>, 'html_name': 'js_lib', 'html_initial_id': u'initial-id_js_lib', 'label': u'Js lib', 'field': <django.forms.models.ModelChoiceField object at 0xa6912ec>, 'help_text': '', 'name': 'js_lib'}{'html_initial_name': u'initial-js_wrap', 'form': <mooshell.forms.ShellForm object at 0xa69168c>, 'html_name': 'js_wrap', 'html_initial_id': u'initial-id_js_wrap', 'label': u'Js wrap', 'field': <django.forms.fields.TypedChoiceField object at 0xa69128c>, 'help_text': '', 'name': 'js_wrap'}"}

我的控制台说:

[18:03:21.744] ReferenceError: reference to undefined property a[o] @ http://html5shim.googlecode.com/svn/trunk/html5.js:5

[18:03:21.744] TypeError: variable b redeclares argument @ http://html5shim.googlecode.com/svn/trunk/html5.js:9

2 个答案:

答案 0 :(得分:2)

这些行有错:

p.innerHTML=h;
span2.innerHTML=p;
r.innerHTML=span1+span2+span3;

innerHTML包含HTML字符串,而不是元素。你也需要在那里使用appendChild

p.appendChild(h);
span2.appendChild(p);
r.appendChild(span1);
r.appendChild(span2);
r.appendChild(span3);

答案 1 :(得分:0)

我最大的问题实际上只是HTML中的onclick部分而不是js。感谢icktoofay的帮助。

相关问题