通过 jQuery 交换表中行 HTML 的最简单方法是什么(不是拖放)
jQuery 中将整个 html 用作 (包括 tr 本身,而不是 tr insidehtml)并将其与另一个进行交换的最简单方法是什么?我正在摆弄replaceWith,但那是交换innerHtml,我想交换整个TR html。看起来这对于 jquery 来说应该是一个简单的任务。我认为必须有一种通过索引引用 TR 的方法,并且就像这样简单:
temp = table.children[4];
table.children[4] = table.children[7]
table.children[7] = temp;
当然这是伪代码,但我正在寻找像 jQuery 中那样简单的东西......
What's the easiest way in jQuery to take the entire html for a <TR>
(including the tr itself, not tr innerhtml) and swap it with another one? I'm messing around with replaceWith, but that's swapping the innerHtml and I want to swap the whole TR html. seems like it should be an easy task for jquery. I'm thinking there's gotta be a way to reference the TRs by index and as easy as:
temp = table.children[4];
table.children[4] = table.children[7]
table.children[7] = temp;
of course that's pseudocode, but I'm looking for something as easy as that in jQuery...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的方法是将其包装在可重用的自定义函数中:
这样您就可以将其用作:
clone使用 (true)
以便事件也被考虑在内。这是一个 SSCCE,它演示了与其下一行(如果有)交换行:
这是一个 SSCCE,演示了如何在您的特定情况:
请注意,元素索引是从零开始的,因此
:eq()
。Nicest would be to wrap it in a reuseable custom function:
So that you can use it as:
The
clone(true)
is used so that events are also taken into account.Here's an SSCCE which demonstrates swapping rows with its next row (if any):
Here's an SSCCE which demonstrates how it can be used in your particular case:
Note that the element index is zero based, hence the
1
and3
in:eq()
.应该这样做:
用简单的英语来说,它在第二行之后插入第一行的副本,然后用第二行替换原始的第一行。
我插入第一行的克隆而不是第一行本身的原因是这样我们就不会丢失第一行的位置并且可以正确定位第二行。
如果我没记错的话,将
replaceWith
与 jQuery 对象一起使用会将元素从原来的位置取出并插入到新位置,但如果情况并非如此,则必须删除targetRow
也。否则,这应该有效。This should do it:
In plain english, it inserts the copy of the first row after the second row, then it replaces the original first row with the second.
The reason why I insert a clone of the first row and not the first row itself is that this way we don't lose the position of the first row and can position the second row properly.
If I remember correctly, using
replaceWith
with a jQuery object will take the element out of it's original place and insert to the new place, but if this is not the case, you then have to remove thetargetRow
also. Otherwise, this should work.基于文档replaceWith (http://docs.jquery.com/Manipulation/replaceWith#content< /a>) 应该做你想做的事,也许你使用的选择器的目标不是 tr ?
Based on the documentation replaceWith (http://docs.jquery.com/Manipulation/replaceWith#content) should do what you want, maybe the selector you are using is targeting something other than the tr?