admin 发表于 2020-10-25 08:38:37

【js】JS生成的动态脚本鼠标事件不响应怎么办?

我用JS生成了一段table,并含<td class="op">多个a</td>
多个a点击可以控制编辑、删除、新建等事件
JS脚本控制事件代码如下:
$("td.op").on("click touchstart", "a", function () {
      var act = $(this).attr("act");
      var page = $("cur").attr("rel");
      if (page === undefined) { page = 1; }
      if (act === "del") { if (!confirm("确定删除此项吗?")) { return; } }
      switch ($(this).attr("method")) {
                case "ajax":
                        getinfo($(this));
                        break;
                default:
                        $.send("", $.extend({ act: $(this).attr("act"), id: $(this).attr("rel"), page: page }, qs));
                        break;
      }
});
发现无响应,经过仔细研究,由于新生成的JS,$("td.op")无法找到,这思路让我找到新的解决方案。
$(document).on("click touchstart", "td.op a", function () {
      var act = $(this).attr("act");
      var page = $("cur").attr("rel");
      if (page === undefined) { page = 1; }
      if (act === "del") { if (!confirm("确定删除此项吗?")) { return; } }
      switch ($(this).attr("method")) {
                case "ajax":
                        getinfo($(this));
                        break;
                default:
                        $.send("", $.extend({ act: $(this).attr("act"), id: $(this).attr("rel"), page: page }, qs));
                        break;
      }
});
页: [1]
查看完整版本: 【js】JS生成的动态脚本鼠标事件不响应怎么办?