代码如下,第二个button代码无效,应该如何写? 具体说这个插件应该如何写? 多谢<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$.fn.myO = function(){
   var i = 0;
   var $t = $(this);
   $t.foo = function(){
       $t.text(++i);
   };
   return $t;
}$(function(){
  var $o = $('#test').myO();
  $("button").eq(0).click(function(){
      $o.foo();
  });
 $("button").eq(1).click(function(){
      $('#test').foo();
  });
});
</script>
</head>
<body>
<button type="button">+1</button>
<button type="button">??</button>
<b id='test'></b>
</body>
</html>

解决方案 »

  1.   

    最简单的方法就是给每个button设置一个id然后通过$("#id值")来获取
      

  2.   

    这样的效果?<button type="button">+<label>0</label></button>
    <button type="button">+<label>0</label></button>
    <button type="button">+<label>0</label></button>
    <script type="text/javascript">
    (function($) {
    $.fn.foo = function() {
    var i = 0;
    $(this).click(function() {
    $('label', this).text(++i);
    });
    return this;
    };
    })(jQuery);
    $('button').foo();
    </script>
      

  3.   


    感谢回复, 可能是我没表达清楚. 
    不是要的效果, 关键是自定义插件相关方法的的写法, 即不改第二个按钮的代码,
    使$('#test').foo()起作用.还有, 如果这个foo放在外面,如何能访问了myO中i的值.
      

  4.   

    感谢回复, 如果这样的话,就等于重新再运行一下插件, 如何能让第二个button的这种写法也能访问到myO中的i值.而不用通过那个变量$o
      

  5.   

    这是类似的实现办法, 但不知如何访问主体中的变量.
    http://docs.jquery.com/Plugins/Authoring#Plugin_Methods