如何将元素添加到不同的< li>
我有这个代码,有一个问题和许多选项。 我有一个无序的列表ul
,我正在尝试在该列表中所有li
中的所有内容,其中每个都包含一个选项名称和复选框。
我的代码目前添加li
,但它添加了复选框及其下的名称,而不是其中。
看起来像这样。
<ul>
<li class="optionName"></li>
<label for="checkbox00">Option 0</label>
<input type="checkbox" id="checkbox00" />
<li class="optionName"></li>
<label for="checkbox01">Option 1</label>
<input type="checkbox" id="checkbox01" />
</ul>
我希望它看起来像这样:
<ul>
<li class="optionName">
<label for="checkbox00">Option 0</label> <input type="checkbox" id="checkbox00" />
</li>
<li class="optionName">
<label for="checkbox01">Option 1</label> <input type="checkbox" id="checkbox01" />
</li>
</ul>
我正在添加HTML的代码看起来像这样:
var option = document.createElement('li');
var checkbox = document.createElement('input');
var label = document.createElement('label');
var checkBoxId = 'checkbox' + '' + questionNumber + subquestionNumber;
checkbox.type = 'checkbox';
checkbox.id = checkBoxId;
option.className = 'optionName';
label.htmlFor = checkBoxId;
checkBoxId.htmlFor = option;
label.appendChild(document.createTextNode('Option ' + subquestionNumber));
q.appendChild(option);
q.appendChild(label);
q.appendChild(checkbox);
I have this code where there is a question and many options.
I have an unordered list ul
and I am trying to all in that list many li
where each one will contain a option name and a checkbox.
My code for the moment add the li
but it add the checkbox and the name under it and not in it.
It looks something like this.
<ul>
<li class="optionName"></li>
<label for="checkbox00">Option 0</label>
<input type="checkbox" id="checkbox00" />
<li class="optionName"></li>
<label for="checkbox01">Option 1</label>
<input type="checkbox" id="checkbox01" />
</ul>
And I want it to look like this:
<ul>
<li class="optionName">
<label for="checkbox00">Option 0</label> <input type="checkbox" id="checkbox00" />
</li>
<li class="optionName">
<label for="checkbox01">Option 1</label> <input type="checkbox" id="checkbox01" />
</li>
</ul>
My code where I am adding the html looks like this:
var option = document.createElement('li');
var checkbox = document.createElement('input');
var label = document.createElement('label');
var checkBoxId = 'checkbox' + '' + questionNumber + subquestionNumber;
checkbox.type = 'checkbox';
checkbox.id = checkBoxId;
option.className = 'optionName';
label.htmlFor = checkBoxId;
checkBoxId.htmlFor = option;
label.appendChild(document.createTextNode('Option ' + subquestionNumber));
q.appendChild(option);
q.appendChild(label);
q.appendChild(checkbox);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该调用
appendchild
onoption
而不是q
You should call
appendChild
onoption
instead ofq
顺便说一句,您可能会发现使用更容易可视化。
我创建了一个函数,您可以将
问卷
和subquestionnumber
参数传递。该功能使用这些参数返回一些HTML。如果您然后使用
querySelector
拾取ul
元素,则可以使用insertadjacenthtml
将调用该函数调用的结果添加到列表中。附加文档
/a>
insertAdjacentAdjacentAdjacentHtAdjacentHtml
/p>Just as an aside you may find working with template strings easier to visualise.
I've created a function into which you can pass the
questionNumber
andsubquestionNumber
arguments. The function returns some HTML using those arguments.If you then use
querySelector
to pick up theul
element, you can then useinsertAdjacentHTML
to add the result of calling the function to the list.Additional documentation
querySelector
insertAdjacentHTML