这是功能,
<script type="text/javascript"> $(document).ready(function() { getRecordspage(1,5); $("a.page-numbers").click(function() { alert(1); getRecordspage($(this).text(),5); return false; }); });
在我的页面中,我动态地将锚点附加到此div,
<div id="pager" class="pager"> //my anchors will be present here... </div>
我动态地附加锚点…所有锚点都有class =“page-numbers”……怎么做…
当通过firebug检查时,我的寻呼机div在我点击3时有这个,
<div class="pager" id="pager"> <a class="page-numbers prev" href="#">Prev</a> <a class="page-numbers" href="#">1</a> <a class="page-numbers" href="#">2</a> <span class="page-numbers current">3</span> <a class="page-numbers" href="#">4</a> <a class="page-numbers next" href="#">Next</a></div>
编辑:
我使用jquery 1.4 …
function getRecordspage(curPage,pagSize) { $.ajax({ type: "POST",url: "Default.aspx/GetRecords",data: "{'currentPage':" + curPage + ",'pagesize':" + pagSize + "}",contentType: "application/json; charset=utf-8",dataType: "json",success: function(jsonObj) { var strarr = jsonObj.d.split('##'); var jsob = jQuery.parseJSON(strarr[0]); var divs = ''; $.each(jsob.Table,function(i,employee) { divs += '<div class="resultsdiv"><br /><span class="resultName">' + employee.Emp_Name + '</span><span class="resultfields" style="padding-left:100px;">Category :</span> <span class="resultfieldvalues">' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" class="resultfields">Salary Basis :</span> <span class="resultfieldvalues">' + employee.SalaryBasis + '</span><span class="resultfields" style="padding-left:25px;">Salary :</span> <span class="resultfieldvalues">' + employee.FixedSalary + '</span><span style="font-size:110%;font-weight:bolder;padding-left:25px;">Address :</span> <span class="resultfieldvalues">' + employee.Address + '</span></div>'; }); $("#ResultsDiv").append(divs); $(".pager").pagination(strarr[1],{ current_page: curPage - 1,items_per_page: '5',num_display_entries : '5',next_text: 'Next',prev_text: 'Prev',num_edge_entries: '1' }); $(".resultsdiv:even").addClass("resultseven"); $(".resultsdiv").hover(function() { $(this).addClass("resultshover"); },function() { $(this).removeClass("resultshover"); }); } });
解决方法
使用
.live()
:
$("a.page-numbers").live('click',function() { alert(1); getRecordspage($(this).text(),5); return false; });
或者在创建链接后执行处理程序分配.如果执行此函数时没有带有类页码的元素,那么当然没有任何反应.
但是live()负责:
Description: Attach a handler to the event for all elements which match the current selector,now or in the future.
更新:
嗯,我不确定.您可以尝试的一件事是将您的函数移动到Ajax调用的成功函数中:
function getRecordspage(curPage,success: function(jsonObj) { // .. lot of stuff here.. // at the end: $("a.page-numbers").click(function() { alert(1); getRecordspage($(this).text(),5); return false; } } }); }