javascript - 为什么我的appendChild 不起作用?

标签 javascript html 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 英雄单元中。消费容器基本上只是一个容器,任何__英雄单位基本上都是一个英雄单位。如果有人可以帮助修复它,我们将不胜感激。谢谢。

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

最佳答案

这些行有问题:

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

关于javascript - 为什么我的appendChild 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16641939/

相关文章:

javascript - 如何从桌面网页隐藏菜单按钮?

javascript - JQuery/Javascript - 将文本与变量组合起来用作数组索引

javascript - "position: fixed"parent旋转和平移时div不固定

javascript - jquery 中动态创建按钮的单击处理程序

java - 执行 XML 转换的最有效方法 - Java

javascript - `innerText` 在 Mozilla Firefox 中未定义

html - 在 HTML block 中显示在 v-model 中输入的 URL

html - 我如何扩展 Eclipse 中着色语法的颜色

javascript - 如何使用php读取html中标签的id?

javascript - 从可能包含子项的 anchor 标记中监听所有 "onClick"事件