gpt4 book ai didi

jquery - 把它变成一个jquery函数?

转载 作者:行者123 更新时间:2023-12-01 07:01:23 24 4
gpt4 key购买 nike

我正在使用下面的代码在表格上进行分页(请不要建议任何预构建的代码)

function tablejax(url, start, count) {
$('#tablejaxButton').attr("disabled", "true");
$('#tablejaxOverlay').fadeIn('slow');
$.ajax({
type: "GET",
url: url,
data: "start="+start+"&count="+count+"&identifier="+$('#tablejaxButton').val(),
success: function(data){
setTimeout(function(){
$('#myTable tbody').append(data);
$('#myTable tr td div').slideDown('slow');
$("html:not(:animated),body:not(:animated)").animate({
scrollTop: $('body').attr("scrollHeight")
}, 1000 );
$("table").trigger("update");
}, 1000);
setTimeout(function(){
$('#tablejaxOverlay').fadeOut('slow');
$('#tablejaxButton').removeAttr('disabled');
}, 1000);
}
});
}

但是我目前正在通过

调用它
$('#tablejaxButton').click(function() {
tablejax(this.name, tcount, 20);
tcount = tcount+20;
})

这有一些问题,但我想改变的是。

我希望能够调用 $('#tableID').tablejax('url', 'identifier', 'count');
例如 $('#tableUsers').tablejax('get_members.php', 27, 20);

它会自动将 onclick 事件附加到 #tableUsersButton(tableID + Button)

我希望函数本身能够跟踪我们应该从哪里开始,目前我正在使用

tcount = 20;  
//do stuff
tcount = tcount +20;

这并不理想。

最简单的方法是什么?

注意:该函数应该假设传递给它的 count 已经加载到表中,例如,如果您调用 $('#tableUsers').tablejax ('get_members.php', 27, 20); 那么前 20 行已经通过服务器端处理加载到表中。

<小时/> 更新
为了简化我的问题。

我需要能够调用 $('#tableID).tablejax(--initial params here--);文件准备好后,

这基本上可以通过填写一些默认参数(例如标识符、url 等)来准备好函数这还会向 $('#tableIDButton') 添加 onclick 事件处理程序,单击该事件处理程序时会调用类似 $('#tableIDButton').tablejax('next');

使用这种方法是因为有时我需要使用另一个触发器调用分页。

我需要知道的是

  • 如何在jquery中注册函数:已解答
  • 如何在调用函数时注册点击事件答案: 事实证明,我什至可以从任何地方注册点击,而不仅仅是准备好文档
  • 当函数不是初始调用时,如何调用函数中的“操作” (初始调用应使用参数,后续调用应调用“内部函数”)

最佳答案

我想这会有所帮助: http://www.google.bg/search?q=jquery+plugin+tutorial

基本上你会写这样的东西:

(function($){
$.fn.tablejax = function(options) {
this = $(this); // this is the element on which we called the func
this.tcount+=20;
// do something here...
}
})(jQuery);

将其保存到jquery.tablejax.js,然后在jquery.js之后的html中调用它
您需要实现该函数,然后可以在每个元素或选择器上调用它。

关于jquery - 把它变成一个jquery函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3543830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com