jQuery-UI draggable error 'cannot call methods

2019-04-20 17:29发布

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:

$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?

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-20 18:23

Expanding on what LeGEC said...

$this.draggable(); is being called before $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

查看更多
手持菜刀,她持情操
3楼-- · 2019-04-20 18:27

I had a similar issue when upgrading from jquery 1.6.1 to 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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-04-20 18:28

The meaning of your error is : $this.draggable('enable'); is called before $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.

查看更多
登录 后发表回答