gpt4 book ai didi

javascript - 如何在选择时使所选行转到表格顶部

转载 作者:行者123 更新时间:2023-12-02 22:54:53 25 4
gpt4 key购买 nike

我只是对将所选行移动到顶部感兴趣 选中复选框时的表格

我实现的唯一一件事是将复选框移到表格顶部,但是当我取消选中复选框时,它们仍保留在顶部

这只是一个文件中的代码,JavaScript 代码位于文件的最底部

    <!DOCTYPE html>
<html>

<head>
<title>HTML Table Row Up And Down</title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
table {
border: 1px solid black;
border-collapse: collapse;
}

#table {
list-style-type: none;
margin: 0;
padding: 0;
width: 60%;
}

#table tr {
margin: 3px;
padding: 0.4em;
font-size: 1.4em;
height: 18px;
}

thead tr th {
background-color: #66e9ff;
font-size: 20px;
border: 2px solid black;
border-collapse: collapse;
}

tbody td {
min-width: 100px;
border: 1px solid black;
}

tbody td:first-child {
text-align: center;
}

tbody tr.checked td {
background-color: #ffbbbb;
color: dark;
}

tbody tr:hover {
background-color: yellow;
}

#feedback {
font-size: 1.4em;
}

table .ui-selecting {
background: #FECA40;
}

table .ui-selected {
background: #928bff;
color: white;
}
</style>
</head>

<body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="Table">
<thead>
<tr>
<th><input type="checkbox" class="check_all" value="Check All"></th>
<th>Header 1</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td>Label 1</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Label 2</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Label 3</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Label 4</td>
</tr>
</tbody>
</table>

<button>&ShortUpArrow;</button>
<button>&ShortDownArrow;</button>

<script>
$(function() {

function setCheck(o, c) {
o.prop("checked", c);
if (c) {
//The closest() method returns the first ancestor of the selected element.
o.closest("tr").addClass("checked");
} else {
o.closest("tr").removeClass("checked");
}
}

$("tbody tr").on("click", function(e) {
var chk = $("[type='checkbox']", this);
setCheck(chk, !$(this).hasClass("checked"));

//moved the checkboxes to the top------------------------
// var $tbody = $("table");
// $tbody.prepend( $tbody.find(chk) );
});
});

</script>
</body>

</ht

ml>

最佳答案

您正在寻找 .prependTo

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$("td").click(function () {
$(this).parent().prependTo("#mytable");
});
</script>
<table id="mytable">
<tr>
<td>row 1</td>
</tr>
<tr>
<td>row 2</td>
</tr>
<tr>
<td>row 3</td>
</tr>
</table>

关于javascript - 如何在选择时使所选行转到表格顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58033440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com