JS
function load() {
html = "";
var tab = $("#content");
$.ajax({
url: "/blog/ablist",
type: "get",
async: false,
dataType: "json",
success: function (json) {
console.log(json);
for (var i = 0; i < json.length; i++) {
console.log(json[i].title);
html += "<div class=\"con1\">\n" +
" <!-- th:href=\"@{/blog/delete/{id}(id=${blog.id})}\"-->\n" +
" <a id=\"delete\" href=\"/blog/delete/" + json[i].id + "\" th:href=\"@{/blog/delete/{id}(id=${" + json[i].id + "})}\">\n" +
" <button style=\"color: red\">删除</button>\n" +
" </a>\n" +
" <a id='lock' href=\"/blog/lock/" + json[i].id + "\"><button id=\"lock\">锁定</button></a>\n" +
" </div>" +
" </div>\n" +
" </div>";
}
$("#content").html(html);
html = "";
}
});
}
方案
使用JQuery的委托事件,将该方法委托到页面已经存在的一个节点上
$("#content").delegate('#delete', 'click', function () {
var b = confirm("数据删除后不可恢复,你确定要删除吗?");
if (b == false) {
return false;
} else {
return true;
}
});