如何交换两个div标签? [英] How to swap two div tags?
问题描述
我想完全交换两个html div标签,即标签和全部.我尝试了下面的代码,但是它不起作用.
jQuery('#AllBlock-'+Id).insertAfter('#AllBlock-'+Id.next().next());
如何完全交换两个div标签.
代码中的括号不匹配,看来您可能正在尝试这样做:
jQuery('#AllBlock-'+Id).insertAfter($('#AllBlock-'+Id').next().next());
会采取类似的方式:
<div id="AllBlock-5"></div>
<div id="AllBlock-6"></div>
<div id="AllBlock-7"></div>
然后,如果使用ID 5进行调用,则将其转换为:
<div id="AllBlock-6"></div>
<div id="AllBlock-7"></div>
<div id="AllBlock-5"></div>
这是因为您正在获取第5块,并将其(使用insertAfter
)移动到距离其自身next().next()
(或下一个但又一个)的块之后的位置,也就是第7块./p>
如果您想始终将#AllBlock-Id
与#AllBlock-[Id+2]
交换,则它们会切换位置并最终如下所示:
<div id="AllBlock-7"></div>
<div id="AllBlock-6"></div>
<div id="AllBlock-5"></div>
您可能想尝试:
var $block = jQuery('#AllBlock-'+Id);
var $pivot = $block.next();
var $blockToSwap = $pivot.next();
$blockToSwap.insertBefore($pivot);
$block.insertAfter($pivot);
I want to swap two html div tags entirely, tags and all. I tried the code below code but it does not work.
jQuery('#AllBlock-'+Id).insertAfter('#AllBlock-'+Id.next().next());
How to swap two div tags entirely.
You have some bracket mismatching in your code, it looks like you might be trying to do this:
jQuery('#AllBlock-'+Id).insertAfter($('#AllBlock-'+Id').next().next());
Which would take something like:
<div id="AllBlock-5"></div>
<div id="AllBlock-6"></div>
<div id="AllBlock-7"></div>
And, if called with Id 5, turn it into this:
<div id="AllBlock-6"></div>
<div id="AllBlock-7"></div>
<div id="AllBlock-5"></div>
This is because you're taking block 5, and moving it (using insertAfter
) to the place after the block that's next().next()
(or next-but-one) from itself, which would be block 7.
If you want to always swap #AllBlock-Id
with #AllBlock-[Id+2]
, so they switch places and end up like the following:
<div id="AllBlock-7"></div>
<div id="AllBlock-6"></div>
<div id="AllBlock-5"></div>
You might want to try:
var $block = jQuery('#AllBlock-'+Id);
var $pivot = $block.next();
var $blockToSwap = $pivot.next();
$blockToSwap.insertBefore($pivot);
$block.insertAfter($pivot);
这篇关于如何交换两个div标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!