Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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 如何制作jQuery';s.offset()工作?_Javascript_Jquery - Fatal编程技术网

Javascript 如何制作jQuery';s.offset()工作?

Javascript 如何制作jQuery';s.offset()工作?,javascript,jquery,Javascript,Jquery,我正在开发一个web应用程序,需要知道元素相对于网页的坐标。使用jQuery的offset()似乎应该很简单,但它对我不起作用 以下是我用来查找偏移量的代码: var offset=$('#container').offset(); alert(offset.right); 以下是我为我想要的职位使用的代码: <div id='container' style='position:absolute; height:30px; width:300px; background-color

我正在开发一个web应用程序,需要知道元素相对于网页的坐标。使用jQuery的offset()似乎应该很简单,但它对我不起作用

以下是我用来查找偏移量的代码:

 var offset=$('#container').offset();
 alert(offset.right);
以下是我为我想要的职位使用的代码:

<div id='container' style='position:absolute; height:30px; width:300px; background-color:orange;'></div>


但是当我加载页面并触发alert()时,它会发出未定义的警报。有人知道我该如何解决这个问题吗?

偏移量的顶部和左侧参数不正确

var offset=$('#container').offset();
alert(offset.left);

偏移具有顶部和左侧参数,而不是右侧参数

var offset=$('#container').offset();
alert(offset.left);
作为注释,offset()显示相对于其最近祖先的顶部和左侧,带有position:relative(我认为它默认为body标记)。

作为注释,offset()显示相对于其最近祖先的顶部和左侧,带有position:relative(我认为它默认为body标记)。

为什么不
$('#container')。position.left
?,所以您不能使用
。right
。为什么不使用
$(“#容器”).position()。left
?,所以您不能使用
。right