假设我有这个 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/