繁体   English   中英

使用 jquery 动态扩展表

[英]Dynamicaly expanding table with jquery

我正在尝试制作一个返回用户选择的行和列号的表 gui。 这是我到目前为止:

HTML

<div id="target">
  <table>

  </table>
</div>

CSS

#target td {
  min-width: 20px;
  width: 20px;
  min-height: 20px;
  height: 20px;
  border: 1px solid black;
}

td.selected {
  background-color: blue;
}

Javascript

    $(document).ready(function() {
  createTable(5, 5);
});

function createTable(rows, cols) {
  var minRows = 5,
    minCols = 5;

  rows = (rows < minRows) ? minRows : rows;
  cols = (cols < minCols) ? minCols : cols;
  var $table = $("<table><tbody></tbody></table>");

  for (var i = 1; i <= rows; i++) {
    var $tr = $("<tr />");
    for (var j = 1; j <= cols; j++) {
      var $td = $("<td />");
      $tr.append($td);
    }
    $table.append($tr);
  }

  $("#target table").replaceWith($table);
  bind();
}

function bind() {
  $("table td").hover(function() {
    var minCols = 5,
      minRows = 5;

    var col = $(this).index();
    var row = $(this).parent().index();

    createTable(row + 2, col + 2);

    $("table td").removeClass("selected");
    var $trs = $("table tr");
    $trs.slice(0, row + 1).each(function(i, tr) {
      if (i > row)
        return false;
      $(tr).find("td").slice(0, col + 1).addClass("selected");
    });
  });
  $("#target").mouseleave(function() {
    console.log("left");
    createTable(5, 5);
  });
  $("table").on("click", "td", function() {
    var col = $(this).index();
    var row = $(this).parent().index();
    console.log(row + ", " + col);
  });
}

小提琴

https://jsfiddle.net/shirandror/efr7dkxL/

所以我遇到了这段代码的两个问题。 首先,当点击 TD 时,点击事件不会触发。 其次,当鼠标离开环绕的 div 时, mouseleave 事件被调用数百次。 我在这里缺少什么?

$("table td").hover(function() {改为$("table td").on("mouseenter", function() {

请检查: https : //jsfiddle.net/efr7dkxL/12/

jQuery

var minCols = 5,
    minRows = 5;

$(document).ready(function() {
    createTable(5, 5);
});

function createTable(rows, cols) {

    rows = (rows < minRows) ? minRows : rows;
    cols = (cols < minCols) ? minCols : cols;
    var $table = $("<table><tbody></tbody></table>");

    for (var i = 1; i <= rows; i++) {
        var $tr = $("<tr />");
        for (var j = 1; j <= cols; j++) {
            var $td = $("<td></td>");

            $tr.append($td);
        }
        $table.append($tr);
    }

    $("#target table").replaceWith($table);

    bind();
}

function bind() {

    $("table td").on("mouseenter", function() {

        var col = $(this).index();
        var row = $(this).parent().index();

        var $trs = $("table tr");
        var $tds = $trs.first().children("td");
        if (($trs.length != (row + 2) && ((row + 2) >= minRows)) || ($tds.length != (col + 2) && ((col + 2) >= minCols))) {
            createTable(row + 2, col + 2);
        }
        $trs = $("table tr");
        $("table td").removeClass("selected");

        $trs.slice(0, row + 1).each(function(i, tr) {
            if (i > row)
                return false;
            $(tr).find("td").slice(0, col + 1).addClass("selected");
        });

    });
    $("#target").off("mouseleave");
    $("#target").mouseleave(function() {
        createTable(5, 5);
    });

}

$(document).on("click", "table td", function() {
    var col = $(this).index();
    var row = $(this).parent().index();
    console.log((row + 1) + "," + (col + 1));
    $('.msg').text('ROW: ' + (row + 1) + ' and COL: ' + (col + 1));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM