我尝试使用单击(‘$.on’)和$.addClass()将边框应用于动态生成的元素,但该类似乎不会应用于第一个单击事件。除此之外,它工作得很好。我做错了什么?
$(document.body).on('click', '.card', function() {
var currentSelection = $(this)
var currentSelectionIndex = $(currentSelection).index()
$(currentSelection).addClass("selected")
if (currentSelectionIndex !== previousSelectionIndex) {
p = $("#searchResponse").children().get(previousSelectionIndex)
$(p).removeClass("selected")
}
previousSelectionIndex = currentSelectionIndex;
});
解决方案:在我的脚本开头给previousSelectionIndex
赋值,这样就解决了这个问题。
https://stackoverflow.com/questions/53658498
复制相似问题