Javascript 如何在for循环中动态创建多个按钮

Javascript 如何在for循环中动态创建多个按钮,javascript,for-loop,Javascript,For Loop,这是我的密码。我想在特定div中附加4个按钮。换句话说,我想将这4个按钮放在“”中。现在它工作了,但他们不在分区内 getmyItems函数是一个包含一系列信息的函数,如:标题、描述、年龄等 帮助 getmyItems(参数、函数(数据){ var mtItem=JSON.stringify(数据); myItem=JSON.parse(mtItem); var Item=document.getElementById('myItems'); 对于(var i=0;i

这是我的密码。我想在特定div中附加4个按钮。换句话说,我想将这4个按钮放在“”中。现在它工作了,但他们不在分区内

getmyItems函数是一个包含一系列信息的函数,如:标题、描述、年龄等

帮助

getmyItems(参数、函数(数据){
var mtItem=JSON.stringify(数据);
myItem=JSON.parse(mtItem);
var Item=document.getElementById('myItems');
对于(var i=0;i
您希望它们位于哪个特定的分区中?到目前为止,通过这种方式,四个按钮位于myItens div中,请参见和下面的代码

var getmyItems = function(data) {
    var item = document.getElementById('myItems');
    for (var i = 0; i < data.length; i++) {
        var result = data[i];

        // creation of the buffer outer div
        var buffer = document.createElement('div');
        buffer.className = 'act-time';

        //creation of de activity-body
        var activity = document.createElement('div');
        activity.className = 'activity-body act-in';

        //creation of the first span
        var arrow = document.createElement('span');
        arrow.className = 'arrow';

        //creation of the most inner div
        var textDiv = document.createElement('div');
        textDiv.className = 'text';

        //creation of the content of the most inner div
        var attribution = '';
        attribution += '<p class="attribution">';
        attribution += '<a href="#">' + result.title + '</a>';
        attribution += result.description;
        attribution += '</p>';

        //initialize the text div
        textDiv.innerHTML = attribution;

        //put the arrow span inside the activity div
        activity.appendChild(arrow);

        // put the text div inside the activity div
        activity.appendChild(textDiv);

        //put the activity inside the buffer div
        // each time appendChild is applied the element is attach tho the end of the target element
        buffer.appendChild(activity);

        var div = document.createElement('div');
        div.appendChild(buffer);
        item.appendChild(div);

        var btn = document.createElement('input');
        btn.setAttribute('type', 'button');
        btn.setAttribute('class', 'btn btn-danger');
        btn.value = "Delete";
        btn.onclick = (function(i) {
            return function() {
                var c = confirm('Are you Sure? ');
                if (c === true) {
                    //do something;
                };
            };
        })(i);

        // now that all div are created you can choose which div you want to put the button inside.
        // in this I chose the buffer.
        buffer.appendChild(btn);

        var showBtn = document.createElement('input');
        showBtn.setAttribute('type', 'button');
        showBtn.setAttribute('class', 'btn btn-primary');
        showBtn.value = "ShowInDetail";
        showBtn.onclick = (function(i) {
            return function() {
                window.location = 'showInDetail.html';
                //do something
            };
        })(i);
        // button is append to the end of the buffer
        buffer.appendChild(showBtn);

        var extendBtn = document.createElement('input');
        extendBtn.setAttribute('class', 'btn btn-warning');
        extendBtn.setAttribute('type', 'button');
        extendBtn.value = "Extend";
        extendBtn.onclick = (function(i) {
            return function() {
                window.location = 'extendItem.html';
                //do something
            };
        })(i);
        // button is append to the end of the buffer
        buffer.appendChild(extendBtn);

        var bookmark = document.createElement('input');
        bookmark.setAttribute('type', 'button');
        bookmark.setAttribute('class', 'btn btn-primary');
        bookmark.value = 'Bookmark';
        bookmark.onclick = (function(i) {
            return function() {
                var p = { user_id: localStorage.getItem('user_id') };
                window.localStorage.setItem('this_item_id', myItem.results[i].item_id);
                //do something
            };

        })(i);
        // button is append to the end of the buffer
        buffer.appendChild(bookmark);
    }
};
var myItem = [{ title: 'person', description: 'familyName' }, { title: 'ohterPerson', description: 'otherFamilyName' }];
getmyItems(myItem);
var getmyItems=函数(数据){
var item=document.getElementById('myItems');
对于(变量i=0;i”;
//初始化文本div
textDiv.innerHTML=属性;
//将箭头范围放在activity div中
活动。追加子项(箭头);
//将文本div放入活动div中
activity.appendChild(textDiv);
//将活动放入缓冲区div中
//每次应用appendChild时,元素都会附加到目标元素的末尾
buffer.appendChild(活动);
var div=document.createElement('div');
div.appendChild(缓冲区);
项目.儿童(分部);
var btn=document.createElement('input');
btn.setAttribute('type','button');
btn.setAttribute('class','btn-btn-danger');
btn.value=“删除”;
btn.onclick=(函数(i){
返回函数(){
var c=确认('你确定吗?');
如果(c==true){
//做点什么;
};
};
})(i) );
//现在,所有div都已创建,您可以选择要将按钮放入其中的div。
//在这里,我选择了缓冲区。
缓冲区。追加子项(btn);
var showBtn=document.createElement('input');
showBtn.setAttribute('type','button');
showBtn.setAttribute('class','btn btn primary');
showBtn.value=“ShowInDetail”;
showBtn.onclick=(函数(i){
返回函数(){
window.location='showInDetail.html';
//做点什么
};
})(i) );
//按钮被附加到缓冲区的末尾
buffer.appendChild(showBtn);
var extendBtn=document.createElement('input');
setAttribute('class','btn-btn-warning');
setAttribute('type','button');
extendBtn.value=“扩展”;
extendDBTN.onclick=(函数(i){
返回函数(){
var getmyItems = function(data) {
    var item = document.getElementById('myItems');
    for (var i = 0; i < data.length; i++) {
        var result = data[i];

        // creation of the buffer outer div
        var buffer = document.createElement('div');
        buffer.className = 'act-time';

        //creation of de activity-body
        var activity = document.createElement('div');
        activity.className = 'activity-body act-in';

        //creation of the first span
        var arrow = document.createElement('span');
        arrow.className = 'arrow';

        //creation of the most inner div
        var textDiv = document.createElement('div');
        textDiv.className = 'text';

        //creation of the content of the most inner div
        var attribution = '';
        attribution += '<p class="attribution">';
        attribution += '<a href="#">' + result.title + '</a>';
        attribution += result.description;
        attribution += '</p>';

        //initialize the text div
        textDiv.innerHTML = attribution;

        //put the arrow span inside the activity div
        activity.appendChild(arrow);

        // put the text div inside the activity div
        activity.appendChild(textDiv);

        //put the activity inside the buffer div
        // each time appendChild is applied the element is attach tho the end of the target element
        buffer.appendChild(activity);

        var div = document.createElement('div');
        div.appendChild(buffer);
        item.appendChild(div);

        var btn = document.createElement('input');
        btn.setAttribute('type', 'button');
        btn.setAttribute('class', 'btn btn-danger');
        btn.value = "Delete";
        btn.onclick = (function(i) {
            return function() {
                var c = confirm('Are you Sure? ');
                if (c === true) {
                    //do something;
                };
            };
        })(i);

        // now that all div are created you can choose which div you want to put the button inside.
        // in this I chose the buffer.
        buffer.appendChild(btn);

        var showBtn = document.createElement('input');
        showBtn.setAttribute('type', 'button');
        showBtn.setAttribute('class', 'btn btn-primary');
        showBtn.value = "ShowInDetail";
        showBtn.onclick = (function(i) {
            return function() {
                window.location = 'showInDetail.html';
                //do something
            };
        })(i);
        // button is append to the end of the buffer
        buffer.appendChild(showBtn);

        var extendBtn = document.createElement('input');
        extendBtn.setAttribute('class', 'btn btn-warning');
        extendBtn.setAttribute('type', 'button');
        extendBtn.value = "Extend";
        extendBtn.onclick = (function(i) {
            return function() {
                window.location = 'extendItem.html';
                //do something
            };
        })(i);
        // button is append to the end of the buffer
        buffer.appendChild(extendBtn);

        var bookmark = document.createElement('input');
        bookmark.setAttribute('type', 'button');
        bookmark.setAttribute('class', 'btn btn-primary');
        bookmark.value = 'Bookmark';
        bookmark.onclick = (function(i) {
            return function() {
                var p = { user_id: localStorage.getItem('user_id') };
                window.localStorage.setItem('this_item_id', myItem.results[i].item_id);
                //do something
            };

        })(i);
        // button is append to the end of the buffer
        buffer.appendChild(bookmark);
    }
};
var myItem = [{ title: 'person', description: 'familyName' }, { title: 'ohterPerson', description: 'otherFamilyName' }];
getmyItems(myItem);