我想知道完成以下操作的最高效和最佳實踐。這種情況很簡單:如果您單擊前導鏈接減少pagenum或增量,如果您單擊下一個鏈接。然後將其發送到一個函數及其父數據。檢查使用jquery點擊哪個類的有效方法
我知道如何做這兩個不同的功能,但我想要一個簡單,快速的方法。
HTML:
<div>
hello world
<a class="prevpg>Previous</a>
<a class="nextpg">Next</a>
</div>
的javascript:
$(document).on('click', '.prevpg, .nextpg', function(){
var element = $(this).parent('div').text();
// if ,prevpg was clicked
somefunction(pagenum--, element)
// else if .nextpg clicked
somefunction(pagenum++,element)
})
function somefunction(pagenum, data){
var test = true;
if(data == 'hello world'){
test = false; }
$.ajax({
url: 'search',
type: 'GET',
data: {'pagenumber':pagenum, testdata: test} ,
dataType: 'json',
success: function(res){
$('body').append(res.data);
}
});
}
錯誤的第6行,你的意思是'element' not'elment'。 – rvighne
在jQuery選擇器中輸入錯字,你的意思是'.nextpg'而不是'nextpg'。 –
sry爲錯別字! – ravi