替换 DOM 元素的位置并保留其事件

发布于 2024-12-29 02:02:53 字数 555 浏览 2 评论 0原文

我正在编写一个 jquery 插件,它获取一个表并允许更改列顺序。
将位于 oldIndex 位置的列放入 newIndex 位置的代码是:

table.find('> thead > tr, > tbody > tr').each(function() {
    var row = $(this);
    var children = row.children();
    var source = $(children[oldIndex ]);
    var destination = $(children[newIndex ]);

    if (oldIndex != newIndex ) {
        destination
            .replaceWith(source)
            .appendTo(row);
    }
});

问题是每个 td 都有来自此代码之外的事件。当使用 replaceWith 时,它会删除这些事件。

有什么想法可以替换 DOM 元素的位置并保留其事件吗?

I am writing a jquery plugin that gets a table and allow to change the columns order.
The code for putting the column that in position oldIndex in position newIndex is:

table.find('> thead > tr, > tbody > tr').each(function() {
    var row = $(this);
    var children = row.children();
    var source = $(children[oldIndex ]);
    var destination = $(children[newIndex ]);

    if (oldIndex != newIndex ) {
        destination
            .replaceWith(source)
            .appendTo(row);
    }
});

The problem is that each td has events that came outside this code. When using replaceWith, it removes those events.

Any idea hoe can I replace position of DOM element and preserve its events?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白馒头 2025-01-05 02:02:53

确保绑定函数附加到要移动的元素。

我建议使用逻辑来交换列,而不是使用 replaceWith.eq 用于选择特定列的索引,< a href="http://api.jquery.com/after/" rel="nofollow noreferrer">.after().before() 用于交换列:

演示: http://jsfiddle.net/SfwXg/

// Indexes are zero-based
var oldIndex = 1;  // = Second column
var newIndex = 2;  // = Third column
var table = $('table');

if (oldIndex != newIndex) {
    if (oldIndex > newIndex) {
        // Let newIndex always be higher than oldIndex
        var tmp = oldIndex;
        oldIndex = newIndex;
        newIndex = oldIndex;
    }
    table.find('> thead > tr, > tbody > tr').each(function() {
//or:table.children('thead,tbody').children().each(function() {
        var row = $(this);
        var children = row.children();
        
        var right = children.eq(newIndex);
        var left = children.eq(oldIndex);
        
        children.eq(newIndex).after(left);
        if (newIndex != oldIndex+1) {
           // If they're next to each other, don't swap the columns
           children.eq(oldIndex+1).before(right);
        }
    });
}

Make sure that the bound functions are attached to the to-be-moved element.

Instead of using replaceWith, I suggest to use logic to swap the columns. .eq is used to select the index of a specific column, .after() and .before() are used to swap the columns:

Demo: http://jsfiddle.net/SfwXg/

// Indexes are zero-based
var oldIndex = 1;  // = Second column
var newIndex = 2;  // = Third column
var table = $('table');

if (oldIndex != newIndex) {
    if (oldIndex > newIndex) {
        // Let newIndex always be higher than oldIndex
        var tmp = oldIndex;
        oldIndex = newIndex;
        newIndex = oldIndex;
    }
    table.find('> thead > tr, > tbody > tr').each(function() {
//or:table.children('thead,tbody').children().each(function() {
        var row = $(this);
        var children = row.children();
        
        var right = children.eq(newIndex);
        var left = children.eq(oldIndex);
        
        children.eq(newIndex).after(left);
        if (newIndex != oldIndex+1) {
           // If they're next to each other, don't swap the columns
           children.eq(oldIndex+1).before(right);
        }
    });
}
叫思念不要吵 2025-01-05 02:02:53

怎么样:

if (oldIndex != newIndex ) {
    var tmp = $('<td>').insertBefore(destination); // create tmp td before destination
    source.after(destination); // move destination after source
    tmp.after(source).remove(); // move source after tmp, remove tmp
}

编辑:上面的代码交换了 2 个 td,这与所要求的不同(移动一个 td)。

无论事件有什么问题,如果您想将 source 移动到 destination 之前,只需执行 source.insertBefore(destination)目的地.之前(源)。在您的代码中,您将 destination 移动到 tr 的末尾。

How about:

if (oldIndex != newIndex ) {
    var tmp = $('<td>').insertBefore(destination); // create tmp td before destination
    source.after(destination); // move destination after source
    tmp.after(source).remove(); // move source after tmp, remove tmp
}

EDIT: the code above swaps 2 tds, which is different that what was asked (move a single td).

Regardless of the problem with events, if you want to move source before destination, simply do source.insertBefore(destination), or destination.before(source). In your code you're moving destination to the end of the tr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文