jQuery - 我如何动态地添加列表项到无序列表? [英] jQuery - How can i dynamically add a list item to an unordered list?
本文介绍了jQuery - 我如何动态地添加列表项到无序列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
似乎是一件简单的事情(虽然我仍然在学习jQuery的内容)。
Seems like a simple thing to do (although im still learning the ins and outs of jQuery).
这是我的HTML:
<div id="fbsignup">
<div class="message messageerror">
<strong>There were errors with your registration:</strong>
<ul></ul>
</div>
</div>
这是我的(尝试)jQuery:
Here's my (attempted) jQuery:
// Check Required Fields.
$('#fbsignup input.required').each(function () {
if ($(this).val().trim() == '') {
$(this).next('em').html('*').show();
$('#fbsignup .message ul').add('li').html('Fields marked with * are required.');
}
});
这是它正在做的DOM:
And this is what it is doing to the DOM:
<div class="message messageerror">
<strong>There were errors with your registration:</strong>
<ul>Fields marked with * are required.</ul>
</div>
它将文本添加到内部html。我想让它添加一个< li>
元素与我设置的内部html - 我以为这是链接如何工作?
It's adding the text to the inner html. I want it to add a <li>
element with the inner html of what i set - i thought this is how chaining works?
这是我希望的HTML:
This is the HTML i want to end up with:
<div class="message messageerror">
<strong>There were errors with your registration:</strong>
<ul>
<li>Fields marked with * are required.</li>
</ul>
</div>
我也尝试过:
$('#fbsignup .message ul').add('<li>Fields marked with * are required.</li>');
绝对没有什么。
什么我失踪了吗
What am i missing? Is the .add
function not the right thing to use here?
推荐答案
您想为此样式使用 .appendTo(...)
。
$(< li />)。appendTo(#fbsignup .message ul)。html(标有*的字段是必需的。);
这篇关于jQuery - 我如何动态地添加列表项到无序列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文