我試圖從一個元素獲得頂部,但我得到這個錯誤,它是什麼意思,我該如何擺脫它?什麼是prevObject,爲什麼我的選擇器返回?
$(".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…]
這發生在jqueryui的drop事件中,當我嘗試將它放入嵌套的droppable中時。
$.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>
顯示您的標記。 –
這是因爲你的頁面中沒有class myelement的元素 –
@ArunPJohny有,$(「。hover」)返回元素和prevObject不允許我使用最高值。 – shuji