简体   繁体   English

Jquery .css('top')返回undefined

[英]Jquery .css('top') returns undefined

Here's a quick rundown on how my app is built: 以下是我的应用程序构建方式的快速概述:

Elements are created dynamically. 元素是动态创建的。 All the elements are draggable and resizable. 所有元素都是可拖动和可调整大小的。 I've created a function that runs when a button is clicked. 我创建了一个在单击按钮时运行的函数。 Here is the function: 这是功能:

$( '#save_canvas' ).click( function(e){
      var _tmpArray = [];  //Temp Array
      var i = 0;

      $( '#canvas' ).children('.elem').each(
      //$('.elem').each(
         function(){
            _tmpArray = [];   //Temp Array
            _id = $(this).attr('id');

            _top = $( '#' + _id ).css('top');
            _left = $( '#' + _id ).css('left');
            _height = $( '#' + _id ).css('height');
            _width = $( '#' + _id ).css('width');
            _tmpArray[0]   = _id;
            _tmpArray[1]   = _height;
            _tmpArray[2]   = _width;
            _tmpArray[3]   = _top;
            _tmpArray[4]   = _left;
         }
      );
   });

The function works perfectly fine for the first element (first child) . 该功能对第一个元素(第一个孩子)完全正常。 but not the remaining ones. 但不是其余的。 Here is a document.write output of the array with 3 elements created: 这是一个创建了3个元素的数组的document.write输出:

== LEGEND == == LEGEND ==
Element Id 元素ID
Height 高度
Width 宽度
Top 最佳
Left 剩下

textblock_1
50px
150px
262px
100px

textblock_2
undefined
undefined
undefined
undefined

textblock_3
undefined
undefined
undefined
undefined

I've searched far and wide online and cannot figure this one out for the life of me...help please? 我在网上进行了广泛的搜索,无法想到这一点我的生活...请帮助吗? :) :)

Do you have IDs set on the subsequent textblocks? 您是否在后续文本块中设置了ID? You do not need to use "$( '#' + _id )" continuously, just use $(this) all the way. 您不需要连续使用“$('#'+ _id)”,只需使用$(this)。

Thanks a lot everyone for the help in finding the issue with this piece of code! 非常感谢大家帮忙找到这段代码的问题!

Ends up the issue was partly because of the way I was calling the elements - I'll have to figure out why still - but also because I had to use .height() and .width(): 结束这个问题部分是因为我调用元素的方式 - 我必须弄清楚为什么还要 - 但也因为我必须使用.height()和.width():

_top = $( this ).css('top');
_left = $( this ).css('left');
_height = $( this ).height();
_width = $( this ).width();

Once again, Thanks! 再一次,谢谢! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM