替换div标签 [英] replacing div tag

查看:256
本文介绍了替换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.

推荐答案

要使用jQuery在另一个元素之后插入一个元素,请使用

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());

这篇关于替换div标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆