最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - binding to click event - event only fires once - Stack Overflow

programmeradmin4浏览0评论

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;
   }

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;
   }
Share Improve this question asked Apr 27, 2011 at 18:39 TacTac 1773 silver badges6 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 9

use live to bind the click event

 $(".aPage").live('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));
});

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));
     });

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.

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

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

评论列表(0)

  1. 暂无评论