Javascript 如何在for循环中动态创建多个按钮
这是我的密码。我想在特定div中附加4个按钮。换句话说,我想将这4个按钮放在“”中。现在它工作了,但他们不在分区内 getmyItems函数是一个包含一系列信息的函数,如:标题、描述、年龄等 帮助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
getmyItems(参数、函数(数据){
var mtItem=JSON.stringify(数据);
myItem=JSON.parse(mtItem);
var Item=document.getElementById('myItems');
对于(var i=0;i;
缓冲区+='';
buffer+=myItem.results[i].说明;
缓冲区+='';
缓冲区+='';
缓冲区+='';
缓冲区+='';
var div=document.createElement('div');
div.innerHTML=缓冲区;
//var elem=div.firstChild;
项目.儿童(分部);
var btn=document.createElement('input');
btn.setAttribute('type','button');
btn.setAttribute('class','btn-btn-danger');
btn.value=“删除”;
btn.onclick=(函数(i){
返回函数(){
var c=确认('你确定吗?');
如果(c==true)
dodelite(myItem.results[i].item_id);
};
})(i) );
项目.附件(btn);
var show_btn=document.createElement('input');
show_btn.setAttribute('type','button');
显示_btn.setAttribute('class','btn btn primary');
show_btn.value=“ShowInDetail”;
show_btn.onclick=(函数(i){
返回函数(){
showInDetail(myItem.results[i]);
location='showInDetail.html';
};
})(i) );
项目.附加子项(显示\u btn);
var extend_btn=document.createElement('input');
扩展_btn.setAttribute('class','btn btn warning');
扩展_btn.setAttribute('type','button');
扩展\u btn.value=“扩展”;
extend_btn.onclick=(函数(i){
返回函数(){
扩展项(myItem.results[i]);
location='extendItem.html';
};
})(i) );
项目.附加子项(扩展\u btn);
var bookmark=document.createElement('input');
bookmark.setAttribute('type','button');
bookmark.setAttribute('class','btn btn primary');
bookmark.value='bookmark';
bookmark.onclick=(函数(i){
返回函数(){
var p={user\u id:localStorage.getItem('user\u id')};
window.localStorage.setItem('this\u item\u id',myItem.results[i].item\u id);
获取书签(p,函数(d){
var bk=JSON.stringify(d);
bk=JSON.parse(bk);
如果(bk.results){
var l=0;
用于(bk.results中的var j){
if(bk.results[j].item_id==localStorage.getItem('this_item_id')){
移除书签(bk.results[j]);
l=1;
}
}如果(l==0){
addBookmark(myItem.results[i]);
}
}否则{
addBookmark(myItem.results[i]);
}
});
};
})(i) );
项目.appendChild(书签);
//document.getElementById(i).appendChild(btn);
}
});
您希望它们位于哪个特定的分区中?到目前为止,通过这种方式,四个按钮位于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);