Javascript 更新到 1.10.1 版时,jQuery-UI 可拖动错误“无法在 init 之前调用方法”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14955630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery-UI draggable error 'cannot call methods prior to init', in updating to version 1.10.1
提问by user961627
I was working the draggable plugin fine while using jQuery-UI 1.8.2, then I changed to 1.10.1. The major difference I found was that in enabling and disabling the plugin, I no longer needed to use:
我在使用 jQuery-UI 1.8.2 时很好地工作了可拖动插件,然后我更改为 1.10.1。我发现的主要区别是在启用和禁用插件时,我不再需要使用:
$this.draggable('option', 'disabled', true);
but could simply use
但可以简单地使用
$this.draggable('disable');
But then I realized there's another problem. I get this error, which messes up my entire program, and I don't know how to fix it:
但后来我意识到还有另一个问题。我收到这个错误,它弄乱了我的整个程序,我不知道如何修复它:
Error: cannot call methods on draggable prior to initialization; attempted to call method 'enable'
错误:无法在初始化之前调用可拖动的方法;试图调用方法“启用”
To fix it, I ensured that I always call $this.draggable('enable');before any further options, but it didn't make a difference. What's the problem?
为了解决这个问题,我确保我总是$this.draggable('enable');在任何进一步的选择之前调用,但这并没有什么区别。有什么问题?
回答by LeGEC
The meaning of your error is : $this.draggable('enable');is called before $this.draggable();.
您的错误的含义是:$this.draggable('enable');在之前被调用$this.draggable();。
Check the execution flow of your progam : make sure that you have indeed initialized the plugin (e.g : called $this.draggable();) before trying to do anything with it.
检查程序的执行流程:确保$this.draggable();在尝试对插件执行任何操作之前确实已初始化插件(例如:已调用)。
回答by nickbarfuss
Expanding on what LeGEC said...
扩展LeGEC所说的......
$this.draggable();is being called before $this.draggable('enable');
$this.draggable();之前被调用 $this.draggable('enable');
For me the solution would be to chain the event like this...
对我来说,解决方案是像这样链接事件......
$this.draggable().draggable('disable');
First declaring that $this is a draggable, then declaring that it is dissabled
首先声明 $this 是可拖动的,然后声明它是禁用的
回答by htrufan
I had a similar issue when upgrading from jquery 1.6.1 to 1.9.1
从 jquery 1.6.1 升级到 1.9.1 时我遇到了类似的问题
var tr$ = $('<tr>', { draggable: 'true' });
threw "cannot call methods on draggable prior to initialization"
抛出“无法在初始化之前调用可拖动的方法”
modified to:
修改为:
var tr$ = $('<tr>');
if(!('draggable' in document.createElement('span'))) {
//handle old browsers
} else {
tr$.attr('draggable', 'true');
}
Posting in case it helps someone else to see it this way.
发布以防它帮助其他人以这种方式看到它。

