我已经创建了一个jQuery函数,并想在click事件上调用它。我的代码如下:
<script type="text/javascript">
(function($){
$.fn.my_function = function(options){
return this.each(function(){
alert("Hello");
});
};
})(jQuery);
$(document).ready(function(){
$('#submit_buttom').my_function();
});
</script>
请帮忙。
由于
答案 0 :(得分:3)
试试这个:
(function($){
$.fn.my_function = function(options){
var callback = function(){
alert("Hello");
//codes goes here
};
return this.each(function(){
$(this).click(callback);
});
};
})(jQuery);
$(document).ready(function(){
$('#submit_buttom').my_function();
});
答案 1 :(得分:3)
<script type="text/javascript">
(function($){
$.fn.my_function = function(options){
return this.each(function(){
$(this).click(function(){
alert("Hello");
});
});
};
})(jQuery);
$(document).ready(function(){
$('#submit_buttom').my_function();
});
</script>
答案 2 :(得分:1)
您可以使用.click()事件处理程序:
$('#submit_buttom').click(function(){
// Can be any function call, but must be within an asynchronous/anonymous function
my_function();
});
答案 3 :(得分:0)
尝试使用.click()函数:described here on the Getting started with jQuery tutorial