开发者

appendTo div between li

I have the following code

<div id="element1" style="display:none"></div>

<ul>
    <li id="item1">Item 1</li>
    <li id="item2">Item 1</li>
</ul>

On click I want to move element1 after item1

<ul>
 开发者_高级运维   <li id="item1">Item 1</li>
    <div id="element1" style="display:none"></div>
    <li id="item2">Item 1</li>
</ul>

If I use

$('#element1').appendTo('#item1'); 

The element append inside li

<li id="item1">Item 1 <div id="element1">...</div></li>

Any help is appreciated


You can use .insertAfter() for this:

$('#element1').insertAfter('#item1'); 

But a <div> in a <ul> isn't valid HTML, you need to insert only a <li> element here, so I'd wrap it first:

$('#element1').wrap('<li />').parent().insertAfter('#item1'); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜