Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试用拉斐尔画圆圈,在非动态时有效,在动态时无效_Javascript_Jquery_Raphael - Fatal编程技术网

Javascript 尝试用拉斐尔画圆圈,在非动态时有效,在动态时无效

Javascript 尝试用拉斐尔画圆圈,在非动态时有效,在动态时无效,javascript,jquery,raphael,Javascript,Jquery,Raphael,在td上进行了一些计算之后,我试图画一个圆。当我运行以下代码时,它可以工作: $('#priority_one').width($('#learning_streams').width()); $('#priority_one').height($('#learning_streams').height()); var priority_one_paper = new Raphael('priority_one', $('#priority_one').width(), $('#priority

在td上进行了一些计算之后,我试图画一个圆。当我运行以下代码时,它可以工作:

$('#priority_one').width($('#learning_streams').width());
$('#priority_one').height($('#learning_streams').height());
var priority_one_paper = new Raphael('priority_one', $('#priority_one').width(), $('#priority_one').height());
var priority_one_circle = priority_one_paper.circle((pos.left).toFixed(0), (pos.top).toFixed(0), (width/2).toFixed(0));
priority_one_circle.attr('stroke','#000000');
但当我尝试使其动态化(td根据用户的输入而变化)时,它就不再工作了。代码:

function circlePriorityOne() {
    //priority_one is a div absolutely positioned over a table called learning_streams
    //sets size of priority_one based off the table learning_streams
    $('#priority_one').width($('#learning_streams').width());
    $('#priority_one').height($('#learning_streams').height());


    //creates the 'paper' to draw the circle on
    var priority_one_paper = new Raphael('priority_one', $('#priority_one').width(), $('#priority_one').height());

    var main = getMax(priority_one_count); //returns the id of the td to circle
    var pos = $('#'+main).position();
    var width = $('#'+main).width();

    //using toFixed() to get rid of decimals
    var priority_one_circle = priority_one_paper.circle((pos.left).toFixed(0), (pos.top).toFixed(0), (width/2).toFixed(0));
    priority_one_circle.attr('stroke','#000000');
}
看到这个有什么问题吗?谢谢。

你说得对。获取相对于父元素的当前位置

您应该改为使用,它获取相对于
文档的当前位置

编辑
我不太确定,因为我还没有测试过它,但它应该能满足你的需要^^

var td = $("#td"),
    pos_td = td.offset(),
    table = td.parents("table"),
    pos_table = table.offset();

// td's position relative to table
console.log("left: " + (pos_td.left - pos_table.left));
console.log("top: " + (pos_td.top - pos_td.top));

“不起作用”。。。不做饭吗?你开车吗?不说“你好”?:它在干什么?控制台中有错误吗?您是否检查了变量(main、pos、width等)?没有出现错误。它应该画一个圆。事实并非如此。我相信td的位置是相对于文档而不是表格来获取的。这将改变圆的位置。我将其更改为获取相对于环绕在桌子上的div标记的位置。我看看这会不会改变什么。忘了回答你剩下的问题了。main、pos和width都给出了预期的值。为了可读性和性能,尽量不要重复调用jQuery。例如,
$(“#优先级_one”)
用于多个位置。将它保存到函数中的变量中,并使用该变量,而不是每次调用它时让jQuery搜索DOM。由于某种原因,position()给了我相对于文档的位置。它是否将td元素视为表的子元素?将表更改为位置:绝对。现在它工作了P谢谢你的帮助,安德烈亚斯。当你让我朝着正确的方向思考的时候,我会把这个设定为答案。