这是函数,
<script type="text/javascript">
$(document).ready(function() {
getRecordspage(1, 5);
$("a.page-numbers").click(function() {
alert(1);
getRecordspage($(this).text(), 5);
return false;
});
});
在我的页面中,我将动态附加 anchor 到该 div,
<div id="pager" class="pager">
//my anchors will be present here...
</div>
我正在动态附加 anchor ...所有 anchor 都将具有class="page-numbers"
...如何完成...
当通过 firebug
检查时,当我点击 3
时,我的寻呼机 div 出现了这个,
<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;
});
或在创建链接之后执行处理程序分配。如果执行此函数时不存在 page-numbers
类的元素,那么当然什么也不会发生。
但是 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, pagSize) {
$.ajax({
type: "POST",
url: "Default.aspx/GetRecords",
data: "{'currentPage':" + curPage + ",'pagesize':" + pagSize + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(jsonObj) {
// .. lot of stuff here..
// at the end:
$("a.page-numbers").click(function() {
alert(1);
getRecordspage($(this).text(), 5);
return false;
}
}
});
}
关于jquery anchor 点击似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2552633/