jQuery 什么是 prevObject,为什么我的选择器会返回它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19697495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:26:33  来源:igfitidea点击:

What is prevObject and why is my selector returning that?

javascriptjqueryoffset

提问by shuji

I'm trying to obtain the top from an element but I'm getting this error, what does it mean and how do I get rid of it?

我正在尝试从元素中获取顶部,但出现此错误,这是什么意思以及如何摆脱它?

$(".hover").offset().top

>Uncaught TypeError: Cannot read property 'top' of undefined

$(".hover")

[div.hover, prevObject: x.fn.x.init[1], context: document, selector: ".hover", jquery: "2.0.3", constructor: function…]
[prevObject: x.fn.x.init[1], context: document, selector: ".hover", jquery: "2.0.3", constructor: function…]

This happens inside the drop event of jqueryui when I try to drop it into a nested droppable.

当我尝试将它放入嵌套的 droppable 时,这发生在 jqueryui 的 drop 事件中。

$.fn.makeDroppable = function(){
    $(this).droppable({
        drop: function(event, ui) {
            console.log($(".hover"));
            console.log($(".hover").offset().top);
            $(".hover").makeDroppable().removeClass("hover");
        },
        over: function(event, ui) {
            $("<div>").addClass("hover").appendTo(this);
        }
    });
}
$(".container").makeDroppable();

<div class="container"></div>

回答by Ami

jQuery returns prevObject if the DOM does not have the element for which jQuery is being run. You might see the element in your source at the run-time however, it is not not bound to the DOM and therefore, it shows prevObject. Try checking your js file again or else paste the code here.

如果 DOM 没有运行 jQuery 的元素,jQuery 将返回 prevObject。您可能会在运行时在源代码中看到该元素,但它并未绑定到 DOM,因此,它显示 prevObject。尝试再次检查您的 js 文件或将代码粘贴到此处。

回答by Northnroro

Although the error is not related to prevObject, I will explain it since it's a question in the title.

虽然错误与 无关prevObject,但我会解释它,因为它是标题中的一个问题。

jQuery .end()

jQuery .end()

End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.

结束当前链中最近的过滤操作,并将匹配元素的集合返回到之前的状态。

To return its previous state, jQuery returns prevObjectwhich contains selected elements before filtering operation, such as .find(), .filter()and .children(), is applied.

要返回其先前的状态,jQuery 会prevObject在应用过滤操作(例如.find().filter()和 )之前返回包含所选元素的哪个.children()

Example

例子

$('#target').append($('div').find('input').remove().end());

$('div')will select all div elements. --- (*1)

$('div')将选择所有 div 元素。--- (*1)

Then, $('div').find('input').remove()will select all input elements inside the selected div elements and delete these input elements.

然后,$('div').find('input').remove()将选中所选 div 元素内的所有 input 元素并删除这些 input 元素。

After that, $('div').find('input').remove().end()will return the elements before .find('input')is called. Therefore, it will return all div elements same as in (*1)except that all input elements inside divs are removed. The returned elements are stored in prevObject.

之后,$('div').find('input').remove().end()将返回.find('input')调用之前的元素。因此,它将返回与(*1) 中相同的所有 div 元素,只是删除了 div 中的所有输入元素。返回的元素存储在prevObject.

Finally, the returned elements (*1)are appended to #target.

最后,返回的元素(*1)被附加到#target.



.end()'s documentation: https://api.jquery.com/end/

.end()的文档:https: //api.jquery.com/end/