使用 Jquery 交换表的行
<tr id="r1">
<td>1</td>
<td>Some Text1</td>
<td>Some Text1</td>
</tr>
<tr id="r2">
<td>1</td>
<td>Some Text2</td>
<td>Some Text2</td>
</tr>
<tr id="r3">
<td>3</td>
<td>Some Text3</td>
<td>Some Text3</td>
</tr>
<tr id="r4">
<td>4</td>
<td>Some Text4</td>
<td>Some Text4</td>
</tr>
<tr id="r5">
<td>5</td>
<td>Some Text5</td>
<td>Some Text5</td>
</tr>
我有一张表,其组织方式如图所示。我正在尝试做的事情是,使用用户输入切换这些表的行。
例如:当用户输入 r1 和 r3 时,这些表的 id 和“一些文本”部分应该更改,并且表应该如下所示。
<tr id="r3">
<td>1</td>
<td>Some Text3</td>
<td>Some Text3</td>
</tr>
<tr id="r2">
<td>1</td>
<td>Some Text2</td>
<td>Some Text2</td>
</tr>
<tr id="r1">
<td>3</td>
<td>Some Text1</td>
<td>Some Text1</td>
</tr>
<tr id="r4">
<td>4</td>
<td>Some Text4</td>
<td>Some Text4</td>
</tr>
<tr id="r5">
<td>5</td>
<td>Some Text5</td>
<td>Some Text5</td>
</tr>
我尝试将一行的值放入临时变量并进行简单的交换,就像 OOP 中一样。然而 Jquery 不让我这样做:)。我可以做什么来解决这个问题?
<tr id="r1">
<td>1</td>
<td>Some Text1</td>
<td>Some Text1</td>
</tr>
<tr id="r2">
<td>1</td>
<td>Some Text2</td>
<td>Some Text2</td>
</tr>
<tr id="r3">
<td>3</td>
<td>Some Text3</td>
<td>Some Text3</td>
</tr>
<tr id="r4">
<td>4</td>
<td>Some Text4</td>
<td>Some Text4</td>
</tr>
<tr id="r5">
<td>5</td>
<td>Some Text5</td>
<td>Some Text5</td>
</tr>
I have a table which is organized as shown. What i'm trying to do, switching the rows of these table with user input.
For example: when user enters r1 and r3 the id's of these tables and "some text" parts should be changed and table should look like this.
<tr id="r3">
<td>1</td>
<td>Some Text3</td>
<td>Some Text3</td>
</tr>
<tr id="r2">
<td>1</td>
<td>Some Text2</td>
<td>Some Text2</td>
</tr>
<tr id="r1">
<td>3</td>
<td>Some Text1</td>
<td>Some Text1</td>
</tr>
<tr id="r4">
<td>4</td>
<td>Some Text4</td>
<td>Some Text4</td>
</tr>
<tr id="r5">
<td>5</td>
<td>Some Text5</td>
<td>Some Text5</td>
</tr>
I tried to take values of one row to a temp variable and make a simple swap just like in OOP. However Jquery didn't let me do it :). What can i do to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要交换两个元素,可以使用以下逻辑:
实现如下:
在前面的示例中,我对 ID 进行了硬编码。假设用户在 ID 为
from
和to
的字段中输入 ID。然后,可以这样调整实现:To swap two elements, the following logic can be used:
The implementation goes as follows:
In the previous sample, I have hard-coded the IDs. Assume that the user enters the IDs in fields with IDs
from
andto
. Then, the implementation can be adjusted in this way:您可以使用 prepend 将所需的行推送到表的头部。
一个简单的实现类似于这个:
You can use prepend to push the wanted rows to the head of the table.
A simple implementation looks like this: