jQuery-无法读取未定义的属性顶部



每当尝试运行此jQuery代码时,我一直在收到此错误:

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

undureck typeError:无法阅读未定义的属性"顶部" 在:1:59

我基本上试图将两个元素彼此对齐,无论屏幕大小都有大小,但对我来说并不好...


我准备在此时服用任何建议。

jQuery对象没有style属性,在jQuery对象上使用css()方法。

$(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;
}); 

您将jQuery对象与DOM元素混淆。只有真正的DOM元素具有style属性;您需要在使用此属性之前访问索引[0]

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

尝试从jQuery

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

最新更新