绑定到单击事件 - 事件仅触发一次

发布于 2024-11-03 20:15:38 字数 897 浏览 9 评论 0原文

我正在使用 jquery 对 HTML 表进行分页。

我有 2 个 CSS 类,aPagethePage, 一个创建分页字符串的 JavaScript 函数, 和 jquery 代码将每个“页面”绑定到 click 事件,以便在单击页面时计算分页字符串。

我的问题是点击事件只触发一次,尽管我希望它在每次单击页面时触发。

非常感谢任何帮助。 谢谢!!

我的 javascript/jquery 代码是:

var thisPage=1; npages=10;
 //initial pagination
  $("#pag").html(showPag(thisPage,npages));

 //  bind pagination string to click event
     $(".aPage").click(function(event){
           var thisPage=$(this).text()
     $("#pag").html(showPag(thisPage,npages));
     });
 // function that returns pagination string
     function showPag(thisPage,npages) {
     p="";
     for(i=1;i<=10;i++){
     if(i==thisPage) {
     p+="<span class='thePage'>"+i+"</span>"
      }
     else {p+="<span class='aPage'>"+i+"</span>" }
     }
     return p;
   }

I'm using jquery to paginate an HTML table.

I have 2 CSS classes, aPage and thePage,
a javascript function that creates a pagination string,
and jquery code to bind each "page" to the click event so that the pagination string is calculated when the page is clicked.

My problem is that the click event only fires once, though I want it to fire each time a page is clicked.

Any help is greatly appreciated.
Thanks!!

My javascript/jquery code is:

var thisPage=1; npages=10;
 //initial pagination
  $("#pag").html(showPag(thisPage,npages));

 //  bind pagination string to click event
     $(".aPage").click(function(event){
           var thisPage=$(this).text()
     $("#pag").html(showPag(thisPage,npages));
     });
 // function that returns pagination string
     function showPag(thisPage,npages) {
     p="";
     for(i=1;i<=10;i++){
     if(i==thisPage) {
     p+="<span class='thePage'>"+i+"</span>"
      }
     else {p+="<span class='aPage'>"+i+"</span>" }
     }
     return p;
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千仐 2024-11-10 20:15:38

使用live绑定点击事件

 $(".aPage").live('click',function(event){
           var thisPage=$(this).text()
     $("#pag").html(showPag(thisPage,npages));
     });

use live to bind the click event

 $(".aPage").live('click',function(event){
           var thisPage=$(this).text()
     $("#pag").html(showPag(thisPage,npages));
     });
離人涙 2024-11-10 20:15:38

由于每次调用 .html() 时都会破坏旧的 .aPage 链接,因此您需要使用 .live().delegate()

使用.live()

$(".aPage").live("click", function(event){
   var thisPage=$(this).text()
   $("#pag").html(showPag(thisPage,npages));
});

使用.delegate()

$("#pag").delegate(".aPage", "click", function(event){
   var thisPage=$(this).text()
   $("#pag").html(showPag(thisPage,npages));
});

Since you are destroying the old .aPage link everytime when you call .html() you will need to either use .live() or .delegate()

Using .live()

$(".aPage").live("click", function(event){
   var thisPage=$(this).text()
   $("#pag").html(showPag(thisPage,npages));
});

Using .delegate()

$("#pag").delegate(".aPage", "click", function(event){
   var thisPage=$(this).text()
   $("#pag").html(showPag(thisPage,npages));
});
心碎无痕… 2024-11-10 20:15:38

您需要将单击事件重新绑定到重新填充页面的范围,或者您可以只使用 jQuery 的委托或实时函数。发生的情况是,您绑定单击事件的方式仅涵盖 aPage 类的现有范围。 Live 或 delegate 将绑定到现有和未来:

$(".aPage").live("click", function(event){
           var thisPage=$(this).text()
     $("#pag").html(showPag(thisPage,npages));
     });

You need to either rebind the click event to the spans you repopuplate the page with or you can just use either jQuery's delegate or live function. What is happening is that the way you are binding the click event only covers existing spans with the class aPage. Live or delegate will bind to existing and future:

$(".aPage").live("click", function(event){
           var thisPage=$(this).text()
     $("#pag").html(showPag(thisPage,npages));
     });
战皆罪 2024-11-10 20:15:38

那么您的点击事件已注册到 .aPage 类的元素而不是 #pag 元素。如果这个带有 .aPage 类的元素恰好位于您的 #pag 元素内(您没有指定,所以我只是在这里猜测),那么当您替换页面的 html 时,它就会被销毁。要解决此问题,请将单击处理程序绑定到#pag。

您还可以尝试以下操作:

$(".aPage").click(function(){...})

更改为

$(".aPage").live('click ',function(){...})

这将绑定到具有该 className 的所有当前和未来元素。

Well your click event is registered to elements of the class .aPage NOT the #pag element. If this element with class .aPage happens to be inside your #pag element (you did not specify, so I am just guessing here), it is destroyed when you replace the html of the page. To fix this bind the click handler to #pag.

You can also try the following:

change

$(".aPage").click(function(){...})

to

$(".aPage").live('click',function(){...})

This will bind to all present and FUTURE elements with that className.

为你鎻心 2024-11-10 20:15:38

从 jQuery 1.7 开始,不再支持 live,推荐使用 delegate。

$("#pag").delegate(".aPage", "click", function(event){
    var thisPage=$(this).text()
    $("#pag").html(showPag(thisPage,npages));  
});

Since jQuery 1.7, live is no longer supported, while delegate is recommended.

$("#pag").delegate(".aPage", "click", function(event){
    var thisPage=$(this).text()
    $("#pag").html(showPag(thisPage,npages));  
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文