我们有以下脚本,当页面第一次加载时,我们呼叫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