看起来很简单(虽然我仍在学习jQuery的各个方面)。
这是我的HTML:
<div id="fbsignup">
<div class="message messageerror">
<strong>There were errors with your registration:</strong>
<ul></ul>
</div>
</div>
这是我(尝试的)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所做的操作:
<div class="message messageerror">
<strong>There were errors with your registration:</strong>
<ul>Fields marked with * are required.</ul>
</div>
这是将文本添加到内部html中。我希望它添加一个<li>
元素,其内部html为我设置的内容 - 我认为这就是链式操作的工作原理?
这是我最终想要的HTML:
<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>');
这个操作完全没有任何作用。
我错过了什么吗?.add
函数不是在这里使用的正确方法吗?