使用jquery

时间:2016-03-22 07:36:14

标签: javascript jquery html

我正在尝试创建一个返回用户所选行和列号的表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事件被调用数百次。我在这里错过了什么?

1 个答案:

答案 0 :(得分:1)

$("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));
});