【发布时间】:2013-05-30 20:34:38
【问题描述】:
我正在尝试使用向上和向下箭头交换两个元素。
JSFiddle 解决方案会很棒!
我的 HTML:
<div class="item">
<div class="content">Some text</div>
<div class="move">
<div class="move-down">down</div>
</div>
</div>
<div class="item">
<div class="content">Some other text</div>
<div class="move">
<div class="move-up">up</div>
<div class="move-down">down</div>
</div>
</div>
<div class="item">
<div class="content">Some text</div>
<div class="move">
<div class="move-up">up</div>
<div class="move-down">down</div>
</div>
</div>
<div class="item">
<div class="content">Some other text</div>
<div class="move">
<div class="move-up">up</div>
</div>
</div>
我最后一次尝试是:
// el is the clicked one.
jQuery('.move').children().click(function(el) {
if (jQuery(el).hasClass('move-down') === true) {
el = jQuery(el).parent().parent();
el.prependTo(el.after(el));
} else {
el = jQuery(el).parent().parent();
el.appendTo(el.before(el));
}
});
我尝试了很多不同的方法来更改项目。我试过 replaceWith()、before() 和 after(),但没有任何效果。
注意: 我已经编写了一个显示正确向上/向下 DIV 的函数。所以第一个和最后一个只能朝一个方向移动。这已经解决了。我也不能使用任何现有的 jQuery 插件。
【问题讨论】:
-
你能做一个小提琴吗?
-
您的“el”变量在这里以两种不同的方式使用。
-
el不是点击的元素,而是事件。this是被点击的元素。