$.fn.disable = function(){
return this.each(function(){
       if(typeof this.disabled != 'undefined') this.disabled = true;
});
}

$.fn.extend({
      disable : function(){
return this.each(function(){
       if(typeof this.disabled != 'undefined') this.disabled = true;
}); 
      }
});

확장메서드의 중요한 규칙은 만약 함수에 특정한 값을 반환할 의도가 없다면 반환값으로 항상 확장 집합을 돌려줘야 한다. 그래야만 새로운 커맨드도 jQuery 커맨드 체인에서 사용할 수 있다. 

<html>
<head>
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript">
  // Your code goes here

  (function($){ //html시작시 실행되지 않고 선언해 놓은 것
$.say = function(what) {alert('I say ' + what);};

$.toFixedWidth = function(value, length, fill){
var result = value.toString();
if(!fill) fill = 0;
var padding = length - result.length;
if(padding < 0){
alert(-padding);
result = result.substr(-padding);
}else{
for( var n = 0; n < padding; n++)
result = fill + result;
}
return result; 
};
  }) (jQuery);


  jQuery(function($) { //html시작시 실행
$('#aaa').bind('click',function(){
$.say('hello');
});

alert($.toFixedWidth(3000, 3, 0));
  });
</script>
<style type="text/css">
    div.aaa { font-weight: bold; }
 </style>
</head>
<body>
<div id='aaa'>jquery</div>
</body>
</html>

+ Recent posts