さまざまな要素に対して同じスクリプトを繰り返すことなく、必要なデータを簡単に取得するための最初の jQuery プラグインを作成しています。プラグインが次のようなものを取得できるようにしたいと思います:指定された要素とその親(後で使用する必要があります)の幅、高さ、位置などを次のような形式で:
$('element').myPlugin(); // will return all
$('element').myPlugin(width); // will return only width
$('element').myPlugin(width, height); // will return width and height
プラグインから適切な形式でデータを取得する方法がわかりません。私はそれを理解しようとしましたが、結果として常に「未定義」または[オブジェクトオブジェクト]を取得します。誰かが私の「[[?]]」部分がどのように見えるべきかの例を教えてくれたらありがたいです:
(function($){
$.fn.myPlugin = function(options) {
var defaults = {
width: this.width(),
height: this.height(),
parentWidth: this.parent().width(),
[[more]]
}
var options = $.extend(defaults, options);
return this.each(function () {
var $this = $(this);
[[?]]
};
})(jQuery);