简体   繁体   English

当其他两个li包含同胞时,如何使用jQuery在两个li之间插入li?

[英]How do I use jQuery to insert an li between two other li's when they contain siblings?

I'm trying to add a list item (Your Information) between two other list items (FirstName and Email), but have been unsuccessful because the two list items contain siblings. 我正在尝试在其他两个列表项(名字和电子邮件)之间添加一个列表项(您的信息),但是由于两个列表项包含同级项而没有成功。 Thanks for your help! 谢谢你的帮助!

<script type="text/javascript">
$(document).ready(function(){
$('<li>Your Information</li>').insertAfter('#FirstName');

});
</script>

<form class="lpeRegForm"><ul class='mktLblLeft'>
<li  class='Field' >
<label>First Name:</label>
<span class='Input'>
<input class='FormText' name="FirstName" id="FirstName" type='text' value=""    maxlength='255' tabIndex='1' />
<span class='FormMsg'></span>
</span>
</li>
<li  class='Field' >
<label>Email Address:</label>
<span class='Input'>
<input class='FormText' name="Email" id="Email" type='text' value=""  maxlength='255' tabIndex='2' />
<span class='FormMsg'></span>
</span>
</li>  
</ul>
</form>

You need to insert it after the li that #FirstName is inside of. 您需要在#FirstName位于其中的li之后插入它。 Eg, 例如,

$('<li>Your Information</li>').insertAfter($('#FirstName').closest('li'));


Edit: this alternative syntax is arguably cleaner: 编辑:这种替代语法可以说更干净:

 $('#FirstName').closest('li').after('<li>Your Information</li>'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不影响兄弟姐妹的情况下更改li中的文本 - How do I change text in li without affecting siblings 如何将所有ul和li保留在父li下:selected,但删除其他导航项 - How do I keep all ul and li's under a parent li:selected, but remove other navigation items 我使用JQuery添加新 <li> 现有项目 <ul> 但是新的 <li> 偏离其他 <li> - I use JQuery to add new <li> item to the existing <ul> but the new <li> deviates from other <li> 我如何遍历li的url读取部分,然后使用Jquery将其作为类添加到li - How do I loop through li's read part of a url then add that as a class to the li with Jquery 怎么用? <li> 对于jquery悬停选择器? - How do use a <li> for a jquery hover selector? jQuery:内部有输入的LI,当焦点完全离开LI及其子元素时,如何隐藏LI - jquery: LI w/ input inside, how to hide LI when focus leaves the LI and it's children completely 如何针对这个李类的兄弟姐妹? - How to target the siblings of this li class? 在jQuery中的倍数li之间插入HTML元素 - Insert HTML element between multiples li in jQuery 当我使用jQuery sortable并开始拖动时,如何在IE8中防止li高度变化 - How do I keep my li height from changing in IE8 when I use jQuery sortable and start dragging 插入<ul></ul into few <li>使用 jQuery - insert <ul></ul into few <li>s with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM