첫 번째 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 = $ (this); – redsquare
이것은 좋은 팁입니다, 감사합니다 –
죄송합니다 ...하지만 그 차이점은 무엇입니까? 타이핑 할 문자는 거의 없습니까? – Fernando