替换div标签
问题描述:
我有以下代码,
<div id=1 class="mydiv"> <span>some data</span></div>
<div id="q-1">data</div>
<div id=2 class="mydiv"> <span>some data</span></div>
<div id="q-2">data</div>
<div id=3 class="mydiv"> <span>some data</span></div>
<div id="q-3">data</div>
<div id=4 class="mydiv"> <span>some data</span></div>
<div id="q-4">data</div>
<div id=5 class="mydiv"> <span>some data</span></div>
<div id="q-5">data</div>
我需要与给定ID的div交换一个同一类.
I need to exchange a div of a given ID with the one after it, with the same class.
答
To insert an element after another element using jQuery, use insertAfter
. Note that it automatically removes the element from its original context:
在这里,您想要这样的东西:
Here, you want something like this:
$('#4').insertAfter('#5');
还请注意,除非您使用的是HTML5文档类型,否则ID不能以数字开头.
Note also that IDs cannot start with a number, unless you are using an HTML5 doctype.
要与下一个元素交换,请使用 next
:
To exchange with the next element, use next
:
var $el = $('#4');
$el.insertAfter($el.next());