2011-07-15 79 views
0
給出

: 的jQuery 1.3.2 jQuery UI的1.7.2的jQuery插件幫助

我發現下面的jQuery的片段,但我與它有問題。

$(document).ready(function() { 
     $.widget("ui.hello", { 
      options: { 
       foo: "bar", 
       baz: "quux" 
      }, 
      _init: function() { 
       var text = this.options.text; 
       this.element.innerHTML("hello " + this.options.foo + " and " + this.options.baz); 
      } 
     }); 

     $("#thing").hello({ foo: "WORLD!" }); 
    }); 

這裏的問題:

this.options.baz); 未定義不應該使用默認值嗎?

來源: http://blog.citrusbyte.com/2010/09/16/jquery-widgets-bringing-sanity/

回答

0

這是我落得這樣做:

<script type="text/javascript">  
     $(document).ready(function() { 
      $.widget("ui.hello", { 

       _init: function() { 
        var text = this.options.text; 
        alert(this.options.baz); 
       } 
      }); 

      $.extend($.ui.hello, { 
       defaults: { 
        baz: "whatever" 
       } 
      }); 
      $("#thing").hello({ foo: "WORLD!" });    
     });   
    </script> 
0

this可能不指由於調用上下文時的_init函數被調用的小部件。嘗試取出this.,只是參考的選項,如下所示:

this.element.innerHTML("hello " + options.foo + " and " + options.baz);