2017-03-04 28 views
-1

財產頂部我已經收到此錯誤,每當嘗試運行這段jQuery代碼:JQuery的 - 不能讀取的不確定

$(document).ready(function(){ 
    $("#Card-Right-Sec").style.top = $("#Card-Left-Sec").style.top; 
}); 

Uncaught TypeError: Cannot read property 'top' of undefined at :1:59

基本上,我試圖彼此對準的兩個元素無論多大的屏幕大小調整,但尚未制定出太多了。我...

我已經準備好採取任何建議在這一點上。

+0

你可以分享你的標記? JSFiddle中的一個小例子更有幫助。 –

回答

2

沒有style屬性jQuery對象,使用css()方法jQuery對象上。

$(document).ready(function(){ 
    $("#Card-Right-Sec").css('top', $("#Card-Left-Sec").css('top')); 
}); 

或者通過指數得到DOM對象或使用get()方法和更新的樣式屬性。

$(document).ready(function(){ 
    $("#Card-Right-Sec")[0].style.top = $("#Card-Left-Sec")[0].style.top; 
}); 
+0

此代碼沒有返回錯誤,但是一旦我嘗試運行它控制檯的元素沒有對齊,這可能是我如何接近這個問題? –

+0

@ Zyad-O:這將應用樣式屬性....邏輯可能有問題... –

1

您正在混淆jQuery對象與DOM元素。只有真正的DOM元素具有style屬性;在使用此屬性之前,您需要訪問索引[0]

$(document).ready(function(){ 
    $("#Card-Right-Sec")[0].style.top = $("#Card-Left-Sec")[0].style.top; 
}); 
1

嘗試.css function從jQuery的

$(document).ready(function(){ 
    $("#Card-Right-Sec").css('top',$("#Card-Left-Sec").css('top')); 
});