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

原创 使用jQuery上下移动元素

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

内容目录

有些时候,我们需要对同辈元素中的指定DOM元素进行上下移动,尤其是在多行表格、有序/无序列表中,交换上下相邻两行的位置是非常普遍的需求。

使用jQuery我们可以更加轻松地完成这项任务。

下面是基于jQuery的向上或向下移动指定元素(在同辈元素中)的jQuery实例扩展函数。

// 扩展jQuery实例函数
jQuery.fn.extend({
	/**
	 * 在同辈元素中向上或向下移动
	 * @param direction 'up'或'down'
	 */
	move: function(direction){
		var me = this;
		var another = null;
		if(direction == 'up'){
			another = me.prev();
			another.before(me);
		}else if(direction == 'down'){
			another = me.next();
			another.after(me);
		}
		return this;
	}
});

/* *** 调用举例  ***/
var $p = $('p#id_2');
var success = $p.move('up');	//移动成功则为true,否则false
  • CodePlayer技术交流群1
  • CodePlayer技术交流群2

0 条评论

撰写评论