通过变量设置jQuery偏移量

时间:2015-01-26 06:22:50

标签: jquery css offset

我试图通过jquery放置一个div,如果我将上下左侧的常量值放入:$(divToAdd).offset({left:TableX [i],top:TableY [i]} ); 而不是数组,我也尝试提醒()TableX [i]的值正在工作。

var divToAdd ;
        var TableX=[] ;
        var TableY=[] ;
    var TableX = <?php echo json_encode($TableX);?> ;
            var TableY=<?php echo json_encode($TableY);?> ;
            for(i = 1 ; i <= <?php echo $num ;?>; i++)
            {divToAdd = $("<div></div>");
            $(divToAdd).addClass('dragged1');
            $(divToAdd).offset({ left:TableX[i]  , top:TableY[i]})  ;
            $('#frame').append(divToAdd);
            }

included this php file :

    <?php 
        include('connect.php') ;
        $cnt_table;
        $TableX = []; 
        $TableY =[];
        $result = mysql_query("SELECT DISTINCT TableX , TableY FROM layout ");
        $num_rows = mysql_num_rows($result);
        $num=$num_rows;
        while($row = mysql_fetch_assoc($result)) { 

                $TableX[$num_rows] = $row['TableX'] ;
            $TableY[$num_rows] = $row['TableY'];
        //echo $TableX[$num_rows]."\n" ;
        //echo $TableY[$num_rows]."\n";
        $num_rows--;

        } 
    ?>

开明的人,引导我通过这个...

1 个答案:

答案 0 :(得分:0)

I have found a solution , Although still did not understand what is wrong with offset() .
The new code is : 

var divToAdd ;
        var TableX=[] ;
        var TableY=[] ; 
        var TableX = <?php echo json_encode($TableX);?> ;
        var TableY=<?php echo json_encode($TableY);?> ;
        for(i = 1 ; i <= <?php echo $num ;?>; i++)
        {
            //alert(TableX[i]);
        divToAdd = $("<div></div>");
        $(divToAdd).css('top' , + TableY[i] );
        $(divToAdd).css('left' , +TableX[i] );
        $(divToAdd).addClass('dragged1');
        $(divToAdd).attr("id","table"+i);
        //$(divToAdd).offset( {left:TableX[i]  , top: TableY[i]} );
        $('#frame').append(divToAdd);
        }

添加的行是:

$(divToAdd).css('top' , + TableY[i] );
$(divToAdd).css('left' , +TableX[i] );

感谢回复的人......