Swap two elements in jQuery

血红的双手。 提交于 2019-12-07 05:56:39

问题


I'm trying to swap two elements using up and down arrows.

A JSFiddle solution would be great!

My 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>

My last try was:

// 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));
    }
});

I've tried a lot of different ways to change items. I've tried with replaceWith(), before(), and after() but nothing worked.

NOTICE: I've already written a function which displays the correct up / down DIVs. So the first and last one can only moved in one direction. That's already solved. I also can't use any kind of existing jQuery plugins.


回答1:


You can do it like this - you need to check e.target not the div with class = move

jQuery('.move').children().click(function (e) { // <-- argument passed in is the event not an element
    var $div = $(this).closest('.item'); // get closest item div
    if (jQuery(e.target).is('.move-down')) { // check if clicked is movedown
        $div.next('.item').after($div); // if it is move after next
    } else {
        $div.prev('.item').before($div);// else move it before previous
    }
});

FIDDLE




回答2:


I'd probably do something like this:

$(document).ready(function () {
    $('.move-down').click(function (e) {
        var self = $(this),
            item = self.parents('div.item'),
            swapWith = item.next();
        item.before(swapWith.detach());
    });
    $('.move-up').click(function (e) {
        var self = $(this),
            item = self.parents('div.item'),
            swapWith = item.prev();
        item.after(swapWith.detach());
    });
});

Here's a working example: http://jsfiddle.net/a6Se4/




回答3:


try:

jQuery('.move > div').on('click', function(event) {
    var item = jQuery(this).closest('div.item');
    if(jQuery(this).hasClass('move-down')) {
        item.prev('div.item').before(item);
    } else {
        item.next('div.item').after(item);
    }
});



回答4:


I know it's solved already but I just wanted to leave this solution I stumbled upon while I was looking for an answer to the same question:

Is there a native jQuery function to switch elements?

jQuery("#element1").before(jQuery("#element2"));
or
jQuery("#element1").after(jQuery("#element2"));

it's practically what Derek suggested though.



来源:https://stackoverflow.com/questions/16845446/swap-two-elements-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!