我想附加一个由 jQuery 动态生成的带有 javascript div 标签元素的 div 标签。我的代码如下所示:
$(".remove_item").click(function(){
$(this).hide("fast", function(){
var id = $(this).parent().attr("id");
var remove_item_id = document.getElementById(id);
var block_element = document.getElementById("block");
block_element.removeChild(remove_item_id);
new_item = $("<div/>");
new_item.attr("id", "item");
new_item.attr("name", "item");
new_item.addClass("div_image");
new_item.append($("<img/>")
.addClass("image")
.attr("src", "/compare/sites/default/files/add_item.jpg")
.attr("height", 50)
.attr("width", 50));
new_item.append($("<span/>")
.addClass("new_item")
.click(function(){
$(this).parent().remove();
}));
block_element.append(new_item);
});
});
用 javascript div 标签附加 jQuery div 标签的代码应该如下所示: block_element.append(new_item);
但是它给出了错误,因为我们无法绑定(bind),因为我在同一行中使用 javascript 和 jQuery。有什么办法吗?
最佳答案
唯一需要改变的是
var block_element = $("#block");
$("#"+remove_item_id).remove();
休息应该照原样工作。
关于javascript - 如何使用 javascript div 标签元素附加由 jQuery 动态生成的 div 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9459999/