JQuery - Widget Public Methods

前端 未结 6 1252
野性不改
野性不改 2020-12-24 11:59

If I create a JQuery widget (code example below), and then define a \"public\" method, is there any other way to call the method other than using the following form?

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 12:47

    jQuery UI widgets use jQuery's $.data(...) method to indirectly associate the widget class with the DOM element. The preferred way to call a method on the widget is exactly what was described by Max...

    $('#list').list('publicMethod');
    

    ...but if you want to field a return value, you'll have better luck calling it this way, via the data method:

    $('#list').data('list').publicMethod();
    

    However, using the second way side-steps the whole jQuery UI widget pattern, and should probably be avoided if possible.

提交回复
热议问题