gpt4 book ai didi

javascript - jQuery:在克隆的 parent 中寻找克隆的 child ?

转载 作者:行者123 更新时间:2023-11-29 10:24:57 26 4
gpt4 key购买 nike

假设我有这个 jQuery 扩展方法:

$.fn.foobar = function() {
var clone = this.parent().clone();
};

获取clone后,如何找到与this相同的克隆子元素?

这行得通吗?

$.fn.foobar = function() {
var clone = this.parent().clone();
var cloneOfThis = clone.find(this);
};

还是这个?

$.fn.foobar = function() {
var clone = this.parent().clone();
var self = this;
var cloneOfThis;
clone.children().each(function() {
var $this = $(this);
if ($this === self) {
cloneOfThis = $this;
}
});
};

最佳答案

你可以尝试给它一些独特的类,可以用来引用正确的元素。

$.fn.foobar = function() {
// Add a class to "this", then clone its parent
var clonedParent = this.addClass("someUniqueClass").parent().clone();
// Reference the new clone of "this" inside the cloned parent,
// then remove the class
var cloneOfThis = clonedParent.find(".someUniqueClass").removeClass("someUniqueClass");
// Remove the class from the original
this.removeClass("someUniqueClass");
};

关于javascript - jQuery:在克隆的 parent 中寻找克隆的 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3631536/

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