您的浏览器过于古老 & 陈旧。为了更好的访问体验, 请 升级你的浏览器
Ready 发布于2014年07月18日 11:37 最近更新于 2019年05月24日 13:51

原创 使用jQuery交换任意两个元素的位置

7289 次浏览 读完需要≈ 2 分钟 jQueryJavaScript

内容目录

在前面的文章中,我们介绍了如何使用jQuery上下交换相邻的两个元素的位置。在这里,我们介绍如何使用jQuery交换任意位置的两个元素的位置。

请直接参考如下详细代码:

// 扩展jQuery实例函数
jQuery.fn.extend({
	/**
	 * 交换任意两个jQuery对象的位置
	 * @param another
	 */
	swap: function(another){
		var me = this;
		var cloneMe = me.clone();
		var temp = $('<span/>');
		another.before(temp);
		me.replaceWith(another);
		temp.replaceWith(cloneMe);
		return this;
	}
});

/* ***调用举例*** */
var $t1 = $("#t1");
var $t2 = $("#t2");
$t1.swap($t2);
  • CodePlayer技术交流群1
  • CodePlayer技术交流群2

0 条评论

撰写评论