如何将<button>附加到<li>? (JavaScript的)</LI> </按钮>

时间:2015-03-03 11:44:56

标签: javascript dom append

每次将项添加到数组时都会运行此函数...它只是创建一个“li”元素,将一些文本附加到该项...然后将其附加到“ul”元素(#requestList) ...

function UpdateListOnScreen(NewListItem){

  var grabList = document.getElementById('requestList');

  var text = ""+ GetCalendarName(NewListItem.calChoice) +" For "+ GetLessonSlot(NewListItem.lessonChoice) + " On " + GetDateInTextForm(NewListItem.date) + "";
  var entry = document.createElement('li');
  entry.id = list.length-1;
  entry.className = "ItemNotChecked";
  entry.appendChild(document.createTextNode(text));
  grabList.appendChild(entry);
}

我正在努力弄清楚的是如何在该List项目上添加“按钮”标签?我希望每个List项都有自己的“按钮”标签,但似乎无法弄清楚如何在将文本附加到li之后向li添加按钮。

希望有道理:S

谢谢:D

2 个答案:

答案 0 :(得分:5)

这是一个如何插入按钮的示例:

&#13;
&#13;
var ul = document.getElementById("list");
var li = document.createElement("li");
li.appendChild(document.createTextNode("Four"));
var button = document.createElement("button");
button.innerHTML = "asdasd";
li.appendChild(button);
li.setAttribute("id","element4");
ul.appendChild(li);
alert(li.id);
&#13;
<ul id="list"></ul>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这应该可以解决问题;

function UpdateListOnScreen(NewListItem){

  var grabList = document.getElementById('requestList');

  var text = ""+ GetCalendarName(NewListItem.calChoice) +" For "+ GetLessonSlot(NewListItem.lessonChoice) + " On " + GetDateInTextForm(NewListItem.date) + "";   var entry = document.createElement('li');   entry.id = list.length-1;   entry.className = "ItemNotChecked";   entry.appendChild(document.createTextNode(text));

  /*Add a button to each LI */
var button = document.createElement('button');
button.innerText = 'Click me!';
entry.appendChild(button);

  grabList.appendChild(entry); }