Javascript 从jQueryUI中获取x,y协调员可拖动
我正在尝试获取我使用jquery ui生成的可拖动div的坐标,在jquery文件中我找到了返回位置的函数,但每当我尝试控制台位置时,我都会得到这个错误,有没有办法获取坐标Javascript 从jQueryUI中获取x,y协调员可拖动,javascript,jquery,jquery-ui,coordinates,draggable,Javascript,Jquery,Jquery Ui,Coordinates,Draggable,我正在尝试获取我使用jquery ui生成的可拖动div的坐标,在jquery文件中我找到了返回位置的函数,但每当我尝试控制台位置时,我都会得到这个错误,有没有办法获取坐标 > Unexpected token ( 这是x,y重试的代码: _getRelativeOffset: function() { if ( this.cssPosition !== "relative" ) { return { top: 0, lef
> Unexpected token (
这是x,y重试的代码:
_getRelativeOffset: function() {
if ( this.cssPosition !== "relative" ) {
return { top: 0, left: 0 };
}
var p = this.element.position(),
scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] );
return {
top: p.top - ( parseInt(this.helper.css( "top" ), 10) || 0 ) + ( !scrollIsRootNode ? this.scrollParent.scrollTop() : 0 ),
left: p.left - ( parseInt(this.helper.css( "left" ), 10) || 0 ) + ( !scrollIsRootNode ? this.scrollParent.scrollLeft() : 0 )
console.log(p.top);
};
}
在另一个php/html文件中,我使用了这个div,我不确定这是否是正确的用法:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#draggable { width: 150px; height: 150px; padding: 0.5em; }
</style>
<script>
$(function() {
$( "#draggable" ).draggable();
});
</script>
#可拖动{宽度:150px;高度:150px;填充:0.5em;}
$(函数(){
$(“#可拖动”).draggable();
});
如果我能这样做,就能得到坐标
在对象声明中不能有
控制台.log
调用。将console.log
放在返回语句上方。请使用包含此代码段的文件的完整内容编辑此问题。\u getRelativeOffset:function(){
是js文件中的第一行吗?是的,它是整个单独文件中的第一行,如果用函数声明替换\u getRelativeOffset:function(){
,那么{u getRelativeOffset:function(){是无效js语法的第一行{-错误应该会消失。我不知道你想用\u getRelativeOffset
函数做什么,但这是另一个问题:)非常感谢你确实消除了错误,你是对的,这是一个完全不同的问题,因为我认为它会破坏位置,但它没有为我真正遇到的麻烦感到抱歉值得赞赏。它仍然会给我相同的错误。我在另一个附加到主文件的文件中有此函数。这可能是问题吗?谢谢你的帮助。我没有看到你提供的代码有其他问题。错误是什么时候出现的?尝试删除(注释掉)一些代码,直到你找到问题的原因。我提供的代码是一个jquery代码,在我使用的可拖动div的附件中。我只添加了控制台,因为我需要知道div的位置。我尝试了你所做的更正。刷新页面后,仍然给我相同的错误,它说:1谢谢你的帮助抱歉,我无法找出问题所在,除非您发布文件的全部内容(而不仅仅是您提供的片段)。如果不可能,请尝试删除(或注释掉)代码片段,直到您找到问题。同时确保浏览器看到更新的js文件(在chrome上:空缓存和硬重新加载)没有什么,但我会尝试发布它
_getRelativeOffset: function() {
if ( this.cssPosition !== "relative" ) {
return { top: 0, left: 0 };
}
var p = this.element.position(),
scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] );
// this should be placed here, not inside your object declaration
console.log(p.top);
return {
top: p.top - ( parseInt(this.helper.css( "top" ), 10) || 0 ) + ( !scrollIsRootNode ? this.scrollParent.scrollTop() : 0 ),
left: p.left - ( parseInt(this.helper.css( "left" ), 10) || 0 ) + ( !scrollIsRootNode ? this.scrollParent.scrollLeft() : 0 )
};
}