开发者

Jquery alert hello Plugin doesnt work

Im trying to make simple Jquery plugin. I went through the Jquery documention and I reduced the layout to make a simple alert. This is my Jquery P开发者_如何学Golugin code.

        (function($){
      $.fn.foo = function(){
        alert("HI");
      };


})(jQuery);

Than on my main page I have the Jquery reference URL and this code

  <script type="text/javascript"src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
  foo();
    });
</script>

I keep getting errors in the debugger SCRIPT5009: 'foo' is undefined

Please help me fix this.


you can invoke the foo method on some element or need to define it differently e.g.

<div id="test" />

Plugin -

(function($){
      $.fn.foo = function(){
        alert("HI");
      };

       $.otherfoo = function(){
            alert('Hiiiii');
        };
})(jQuery);

Test -

$(document).ready(function(){
  $('#test').foo();
  $.otherfoo();  
});

should work fine.


You made a jquery plugin, you have to reference it in seperate jQuery function to fire it.

Here is my working example: http://jsfiddle.net/MarkKramer/Z9vz2/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜