0
建立一個購物清單應用程序,當點擊,穿過指定的項目。如果再次單擊該項目,應該將其恢復到原始狀態。我有一個if/else語句來檢查「ixtem」類是否已經被應用。出於某種原因,如果有名單上的多個項目中,if/else語句運行多次,即使我用$(本)選擇:jQuery點擊功能間歇性地與if/else
$('.crossout').click(function() {
if ($(this).closest('.newitem').hasClass('xitem')) {
$(this).closest('.newitem').removeClass('xitem');
console.log('remove x');
}
else {
$(this).closest('.newitem').addClass('xitem');
console.log('x out');
}
})
你可以發佈相關的HTML? – tymeJV
呃,那裏有'toggleClass'嗎? – adeneo
你有多個附加了多個點擊事件處理程序的.crossout元素? – cjsmith