jQuery – 控制 Table 中 Row 的順序
當想要控制 Table 中 Row 的順序時,在其 jQuery 的部份如何實作呢?在此提供實作的程式碼供參考.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#testGrid > tbody > tr").bind("click", function() {
$("#testGrid > tbody > tr").css("background-color", "").removeAttr("selected");
$(this).css("background-color", "red").attr("selected", "true");
});
$("#top").bind("click", function() {
if ($("#testGrid > tbody > tr[selected=true]").size() > 0) {
if (!$("#testGrid > tbody > tr:first").attr("selected"))
$("#testGrid > tbody> tr:first").before($("#testGrid > tbody > tr[selected=true]"));
}
});
$("#bottom").bind("click", function() {
if ($("#testGrid > tbody > tr[selected=true]").size() > 0) {
if (!$("#testGrid > tbody > tr:last").attr("selected"))
$("#testGrid > tbody> tr:last").after($("#testGrid > tbody > tr[selected=true]"));
}
});
$("#up").bind("click", function() {
if ($("#testGrid > tbody > tr[selected=true]").size() > 0) {
if (!$("#testGrid > tbody > tr:first").attr("selected"))
$("#testGrid > tbody> tr[selected=true]").prev().before($("#testGrid > tbody > tr[selected=true]"));
}
});
$("#down").bind("click", function() {
if ($("#testGrid > tbody > tr[selected=true]").size() > 0) {
if (!$("#testGrid > tbody > tr:last").attr("selected"))
$("#testGrid > tbody> tr[selected=true]").next().after($("#testGrid > tbody > tr[selected=true]"));
}
});
$("#ok").bind("click", function() {
$("#testGrid > tbody > tr").each(function(index, element) {
if ($("input:checkbox", this).attr("checked")) {
alert(index + $("td:first", this).text());
}
});
});
});
</script>
</head>
<body>
<table id="testGrid" border="1">
<thead>
<tr>
<th>Test Item</th><th>Fixed</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td><input type="checkbox" /></td></tr>
<tr><td>2</td><td><input type="checkbox" /></td></tr>
<tr><td>3</td><td><input type="checkbox" /></td></tr>
<tr><td>4</td><td><input type="checkbox" /></td></tr>
<tr><td>5</td><td><input type="checkbox" /></td></tr>
</tbody>
</table>
</body>
</html>
留言
張貼留言