我們有以下腳本,當頁面第一次加載時,我們呼叫FindPromo()
此功能特別等待HTML Table
的子代加載。Jquery函數僅適用於頁面加載,在AJAx加載數據後無法運行
然後,它查找所有td's
與類.promotion
,並在同一時間檢查,以確保td
目前在用戶觀看通過調用checkvisible
如果是那麼我們分配額外promoSelect
它,我們也把這個當用戶滾動瀏覽表格,如果他們滾動並看到類別爲promotion
的td
,我們再次將promoSelect
分配給它。
我們再調用GetPromo
這個循環中的table
發現有類promoSelect
和檢索td's
是從它的值 - 到目前爲止,這工作完全正常。
問題:
在表的頂部,有4 buttons
,4 buttons
允許user
加載它通過AJAX
做不同的數據,這又再次填充html
表,當user
按一個buttons
我連線了一個點擊事件再次呼叫FindPromo()
,但它似乎並沒有將promoSelect
類附加到任何td's
具有類.promotion
類數據已成功加載後,即使我已檢查td's
我可以看到幾個哈等級.promotion
。
現在我已經放在console.logs
內FindPromo()
/GetPromo()
他們似乎寫到控制檯,但是腳本失敗後user
推動了4個按鈕中的一個運行。
腳本:
// Called on page load and when the user clicks on 1 of the four buttons
function FindPromo() {
var checkExist = setInterval(function() {
var length = $('table.tst-orderProductsTable').find('tbody').children().length;
if (length > 0) {
clearInterval(checkExist);
$('tbody').scroll(function() {
$('.promotion').filter(checkVisible).addClass('promoSelect'); // Find all promotion td's and assign promoSelect class to them
GetPromo();
}).scroll();
}
}, 500);
}
function checkVisible() {
var elm = this;
var eval = eval || "visible";
var vpH = $(window).height(), // Viewport Height
st = $(window).scrollTop(), // Scroll Top
y = $(elm).offset().top,
elementHeight = $(elm).height();
if (eval == "visible") return ((y < (vpH + st)) && (y > (st - elementHeight)));
}
// Now loop the td's and find all td's that have the class promoSelect.
// Then retrieve information from that td
function GetPromo() {
var product_description = '';
var product_position = '';
$("td").each(function (i, row) {
if ($(this).hasClass('promoSelect')) {
console.log("here");
}
});
}
// clicking 1 of the four buttons above table.
$('.product-tab-label').click(function() {
FindPromo();
});
FindPromo();
究竟你通過表示「無法運行」?控制檯上是否有錯誤?當你調試時,調用函數時會發生什麼?它具體如何失敗? – David
@David當我說運行失敗時,在用戶按下四個按鈕之一後,它沒有找到所有類別提升的td。然而,在頁面加載時,它工作得很好。 –
因此,在回調'.scroll()'選擇器'$('。promotion')'時不會返回匹配的元素?這強烈暗示當時頁面上不存在這樣的元素。它們是以某種方式被動態修改或從DOM中卸載/重新加載的? – David