代码如下,第二个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>
<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>
<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>
感谢回复, 可能是我没表达清楚.
不是要的效果, 关键是自定义插件相关方法的的写法, 即不改第二个按钮的代码,
使$('#test').foo()起作用.还有, 如果这个foo放在外面,如何能访问了myO中i的值.
http://docs.jquery.com/Plugins/Authoring#Plugin_Methods