gpt4 book ai didi

jquery - 使用 append 元素作为 "this"- jQuery

转载 作者:太空宇宙 更新时间:2023-11-04 03:33:06 25 4
gpt4 key购买 nike

我有以下 HTML 和 jQuery 代码:

HTML

<div id="overlay">
<button onclick="startEdit()" class="edit_button">Start Editing</button>
</div>

jQuery

$(".product").click(function() {
var imageName = $(this).data("image");
console.log(imageName);
var imageItem = $("#overlay").append("<div class='product-container'><img class='product-image' src='" + imageName + ".png'></div>");

var ele = imageItem;

ele.draggable({});
// (Theres more than this, but this explains what I'm trying to do)
}

我想要实现的是使用附加元素,在本例中是分配给 imageItem (div.product-container) 的元素,进一步向下代码。

我需要做什么才能像 $(this) 那样使用它,因为我需要进一步向下运行一些适用于该特定元素的代码。

最佳答案

What I'm trying to achieve is to use the appended element, in this case what is assigned to imageItem (div.product-container), further down the code.

但这不是分配给 imageItem 的内容; imageItem 包含 #overlay 元素。

相反:

var imageItem = $("<div class='product-container'><img class='product-image' src='" + imageName + ".png'></div>");
$("#overlay").append(imageItem);
// Or:
// imageItem.appendTo("#overlay");

现在 imageItem 包含 div.product-container,您可以在其他地方使用它。

关于jquery - 使用 append 元素作为 "this"- jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26013474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com