私は最初のjqueryプラグインを構築しようとしていますが、基本的にはマウスオーバー/クリックステートなどでボタンからdivを作成します。次のコードは基本ボタンで機能します。 '通常の'クラスを置き換えるクラスを割り当てます。メソッドが呼び出されますが、私はオプションを読み取ることはできませんか?また、クラス名(addClass)をハードコーディングして割り当てた場合、上およびクリック状態のマウスイベントが失われるようです。jqueryプラグインのカスタムメソッド
コード:私はそれはあなたが達成しようとしている何であるかを正確にはわからない
(function(jQuery) {
jQuery.fn.divbutton = function(options)
{
// default settings
var options = jQuery.extend(
{
width: '75px', // button width
height: '25px', // button height
normal_class: 'brighterbutton', // normal state class
highlight_class: 'brighterbutton-highlight', // normal state class
mouseover_class: 'brighterbutton-mouseover', // mouseover class
mousedown_class: 'brighterbutton-mousedown', // mousedown class
highlighted: false
},
options);
this.each(function()
{
jQuery(this).addClass(options.normal_class);
jQuery(this).width(options.width);
jQuery(this).height(options.height);
jQuery(this).mouseover(function() {
jQuery(this).addClass(options.mouseover_class);
});
jQuery(this).mouseout(function() {
jQuery(this).removeClass(options.mouseover_class);
jQuery(this).removeClass(options.mousedown_class);
});
jQuery(this).mousedown(function() {
jQuery(this).addClass(options.mousedown_class);
});
jQuery(this).mouseup(function() {
jQuery(this).removeClass(options.mousedown_class);
});
});
// public methods
this.doHighlight = function()
{
alert("this doesnt get called");
return this;
};
return this;
};
jQuery.fn.highlight = function()
{
alert("this gets called");
return this.each(function()
{
//this.removeClass(this.options.normal_class);
//this.addClass(this.options.highlight_class);
});
};
})(jQuery);
jQuery(this)を複数回実行しないでください。これをvarに保存します。 var $ this = $(これ); – redsquare
これは良いヒントです、 –
申し訳ありません...しかし、違いは何ですか?入力する文字はほとんどありませんか? – Fernando