jquery hover移到动态添加的区域,显示异常

$("#post-buy-table").find("tr:gt(0)").hover(function(){
    var $itemEditIcon = $("<td class='item-edit-icon'><span class='glyphicon glyphicon-edit'></span><span class='glyphicon glyphicon-trash'></span></td>");
    $(this).find("td:nth-child(3)").replaceWith($itemEditIcon);
},function () {
    $(this).find("td:nth-child(3)").replaceWith("<td></td>");
});

clipboard.png

阅读 2.9k
2 个回答

动态添加的元素用on去绑定事件

$("#post-buy-table").on("hover","tr",function(){
})
$("#post-buy-table").find("tr:gt(0)").hover(function(){
    var $itemEditIcon = $("<a href='#' class='glyphicon glyphicon-edit'></a><a href='#' class='glyphicon glyphicon-trash'></a>");
    $(this).find("td:nth-child(3)").append($itemEditIcon);
},function () {
    $(this).find("td:nth-child(3)").empty();
});

用这些代码写就正常了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进