Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择时,如何使所选行位于表的顶部_Javascript_Jquery_Html - Fatal编程技术网

Javascript 选择时,如何使所选行位于表的顶部

Javascript 选择时,如何使所选行位于表的顶部,javascript,jquery,html,Javascript,Jquery,Html,我只对将所选行移动到列表顶部感兴趣 选中复选框时的表格 我实现的唯一一件事是将复选框移到表的顶部,但当我取消选中复选框时,它们仍保持在顶部 这是一个文件中的代码,javascript代码位于文件的最底部 <!DOCTYPE html> <html> <head> <title>HTML Table Row Up And Down</title> <meta charset="win

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

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

这是一个文件中的代码,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>

上下HTML表格行
桌子{
边框:1px纯黑;
边界塌陷:塌陷;
}
#桌子{
列表样式类型:无;
保证金:0;
填充:0;
宽度:60%;
}
#表tr{
保证金:3倍;
填充:0.4em;
字号:1.4em;
高度:18px;
}
thead tr th{
背景色:#66e9ff;
字体大小:20px;
边框:2件纯黑;
边界塌陷:塌陷;
}
tbody td{
最小宽度:100px;
边框:1px纯黑;
}
tbody td:第一个孩子{
文本对齐:居中;
}
t车身tr.td{
背景色:#ffbbbb;
颜色:深色;
}
t车身tr:悬停{
背景颜色:黄色;
}
#回馈{
字号:1.4em;
}
表.用户界面选择{
背景#FECA40;
}
表1.所选的用户界面{
背景#928BF;
颜色:白色;
}
标题1
标签1
标签2
标签3
标签4
&短向上箭头;
&短向下箭头;
$(函数(){
功能设置检查(o,c){
o、 道具(“已检查”,c);
如果(c){
//方法的作用是:返回所选元素的第一个祖先。
o、 最近的(“tr”)。addClass(“选中”);
}否则{
o、 最近的(“tr”)。删除类(“选中”);
}
}
$(“tbody tr”)。在(“单击”上,函数(e){
var chk=$(“[type='checkbox']”,this);
setCheck(chk,!$(this).hasglass(“checked”);
//将复选框移到顶部------------------
//var$tbody=$(“表格”);
//$tbody.prepend($tbody.find(chk));
});
});

您正在寻找的。prependTo


$(“td”)。单击(函数(){
$(this.parent().prependTo(“#mytable”);
});
第1行
第2排
第3排

如果您将
chk
更改为
chk.closest(“tr”)
,则注释掉的方法将有效。问题是您只移动了复选框而不是整行。非常感谢!这很有效。