为什么这个Javascript DOM方法没有'不起作用
Why this Javascript DOM method doesn't work?
基本上,我正在尝试创建一个动态列表,允许用户添加列表项/删除列表项。
问题是由于某种原因
1) 此变量中的"createTextNode"不起作用。2)
document.getElementById("myList").appendChild(list ).appendChild(inList);
工作,而
var listContainer = document.getElementById("myList").appendChild(list ).appendChild(inList);
listCotnainer.appendChild(list).appendChild(inList);
没有!!
如有任何帮助,我们将不胜感激。谢谢
此外,如果您能指导我使用一个易于编写的动态列表代码,我将不胜感激,这将为我节省大量时间。谢谢
function addItem(txt) {
var list = document.createElement("li");
var listAtr = list.setAttribute("class", "list-group-item");
listAtr.createTextNode(txt);
var inList = document.createElement("button");
var inListAtr = inList.setAttribute("class", "btn btn-default glyphicon glyphicon-minus");
document.getElementById("myList").appendChild(list ).appendChild(inList);
//listCotnainer.appendChild(listAtr);
}
function removeItems() {
}
function removeItem() {
}
<section>
<div ng-controller="addElements">
<h3>{{subtitle}}</h3>
<button class="btn btn-default glyphicon glyphicon-plus" onclick="addItem('This is a test text')"></button>
<button class="btn btn-default glyphicon glyphicon-minus" onclick="removeItems()"></button>
<ul class="list-group" id="myList">
<!-- dynamically generate list items here -->
</ul>
</div>
</section>
您的变量listContainer
错误。应该是:
var listContainer = document.getElementById("myList");
listCotnainer.appendChild(list).appendChild(inList);
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- Meteor-添加用户自定义字段的方法不起作用
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- 对象文字方法上的Javascript绑定不起作用
- jQuery css可见性在load方法中不起作用
- 为什么jQuery悬停方法在这种情况下不起作用
- ng显示“;调用方法“;不起作用
- 在 JQUERY 中创建 HTML 后,Jquery 方法不起作用
- 下拉 jquery 方法发布表单不起作用
- 为什么push方法没有'在这种情况下不起作用:[].推
- dojo/request PUT方法dosn'不起作用
- Jquery事件绑定获胜'当作为方法调用时不起作用,但当直接在控制台中调用时会起作用
- html视频javascript播放方法在移动Safari中不起作用
- 为什么这个扩展方法不起作用
- 我需要一种自动调整iframe大小的方法,无论我查到什么,它都不起作用
- jQuery切换方法的动画不起作用
- Angularjs 跨源资源共享 (CORS) 发布或放置方法在 IE8 和 IE9 中不起作用
- 在node_redis上实现findBy方法,但它没有'不起作用
- Splice()方法不起作用
- 使用replace方法替换innerHTML以及在Javascript中不起作用的正则表达式