gpt4 book ai didi

加载后 jQuery 事件不起作用

转载 作者:行者123 更新时间:2023-12-01 04:20:18 24 4
gpt4 key购买 nike

$(document).ready(function(){

$(function() {
$('a.ajaxload').click(function(e) {
var url = $(this).attr('href');
$('#desktopcontainer').load(url); // load the html response into a DOM element
e.preventDefault(); // stop the browser from following the link
});
});

$(function() {
$(".accordion .accordion-tabs .tab").each(function(){
$(this).click(function(){
if ($(this).hasClass('tab')){
$(this).removeClass('tab');
$(this).addClass('active');
}else{
$(this).removeClass('active');
$(this).addClass('tab');
}

$(this).next().slideToggle('slow');
return false;
});
});
});
});

我的选项卡工作正常,但在我单击“a.ajaxload”将内容添加到页面后,我的选项卡不再响应。

谁能告诉我问题出在哪里吗?

解决了!!!

我所做的是在加载后添加该函数...查看下面的新代码并查看差异。我希望它对某人有帮助。

$(document).ready(function(){

initDashboard();

$(function() {
$('a.ajaxload').click(function(e) {
var url = $(this).attr('href');
$('#desktopcontainer').load(url); // load the html response into a DOM element
e.preventDefault(); // stop the browser from following the link
initDashboard();
});
});

function initDashboard() {
$(".accordion .accordion-tabs .tab").each(function(){
$(this).click(function(){
if ($(this).hasClass('tab')){
$(this).removeClass('tab');
$(this).addClass('active');
}else{
$(this).removeClass('active');
$(this).addClass('tab');
}

$(this).next().slideToggle('slow');
return false;
});
});
}

});

最佳答案

您需要on(),因为它是动态添加的(例如通过load方法插入):

$('.accordion .accordion-tabs').on('click', '.tab', function(){
if ($(this).hasClass('tab')) {
$(this).removeClass('tab');
$(this).addClass('active');
}else{
$(this).removeClass('active');
$(this).addClass('tab');
}

$(this).next().slideToggle('slow');
return false;
});
});

也不需要使用 jQuery 就绪处理程序三次,只需将所有 jQuery 相关代码放入其中即可:

$(document).ready(function(){

});

文档:

关于加载后 jQuery 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11243682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com