我目前正在尝试对一个对象使用 jquery“查找”方法两次,但它不允许我这样做,只有“查找”的第一个实例有效。这是我想要运行的代码...
$("#phone_numbers > p:first-child").clone(true).insertBefore("#phone_numbers > span:last-child").find('.phone_type, .phone_number, .user_phones_id').val('').find('.delete_phone').remove();
除了最后一个“find”方法,上面的代码工作得很好,它没有删除带有“.delete_phone”类的元素。
但是,如果我将代码更改为如下所示...
$("#phone_numbers > p:first-child").clone(true).insertBefore("#phone_numbers > span:last-child").find('.delete_phone').remove();
它确实删除了带有“.delete_phone”类的元素。我认为这是因为我不能连续两次使用“查找”方法,但我不确定。
有谁知道这是怎么回事,或者是否有办法解决这个问题?
谢谢!
最佳答案
你需要一个 .end()
跳回到链中(这样你就不会查看之前的 .find()
结果),像这样:
$("#phone_numbers > p:first-child").clone(true).insertBefore("#phone_numbers > span:last-child").find('.phone_type, .phone_number, .user_phones_id').val('').end().find('.delete_phone').remove();
分割 View :
$("#phone_numbers > p:first-child").clone(true)
.insertBefore("#phone_numbers > span:last-child")
.find('.phone_type, .phone_number, .user_phones_id') //look in the cloned <p>
.val('') //empty those inputs
.end() //hope back to the <p>
.find('.delete_phone') //look in the clone <p>
.remove(); //remove those elements
关于javascript - jQuery:不能在单个对象上两次使用 "find"方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4269261/