Javascript on()不';我认不出一个班级
我在()上遇到了一个问题Javascript on()不';我认不出一个班级,javascript,jquery,html,Javascript,Jquery,Html,我在()上遇到了一个问题on。这是我的密码: <span class="add" id="id_foo" >add this stuff</span> 类remove的选择器无法识别它,而当我用add类单击按钮时,该类正在更改。你知道问题出在哪里吗 谢谢你的帮助 在动态更改处理程序附加到的类时,需要使用委托事件处理程序。试试这个: $(document).on('click', ".add", function() { $.post("/toto.com/add
on
。这是我的密码:
<span class="add" id="id_foo" >add this stuff</span>
类remove
的选择器无法识别它,而当我用add
类单击按钮时,该类正在更改。你知道问题出在哪里吗
谢谢你的帮助 在动态更改处理程序附加到的类时,需要使用委托事件处理程序。试试这个:
$(document).on('click', ".add", function() {
$.post("/toto.com/add.php", { trainning: this.id });
$(this).html("Remove this stuff").toggleClass("add remove");
});
$(document).on('click', ".remove", function() {
$.post("/toto.com/remove.php", { trainning: this.id });
$(this).html("add this stuff").toggleClass("remove add");
});
根据此JS代码所在的
脚本标记的位置,您可能还需要将代码包装到document.ready处理程序中。如果它就在
标记之前,它就可以正常工作,如果它在头部,则将它放在$(function(){/*您的代码在这里*/})中代码>在动态更改处理程序附加到的类时,需要使用委托事件处理程序。试试这个:
$(document).on('click', ".add", function() {
$.post("/toto.com/add.php", { trainning: this.id });
$(this).html("Remove this stuff").toggleClass("add remove");
});
$(document).on('click', ".remove", function() {
$.post("/toto.com/remove.php", { trainning: this.id });
$(this).html("add this stuff").toggleClass("remove add");
});
根据此JS代码所在的脚本标记的位置,您可能还需要将代码包装到document.ready处理程序中。如果它就在
标记之前,它就可以正常工作,如果它在头部,则将它放在$(function(){/*您的代码在这里*/})中代码>尝试将jQuery放在DOM就绪的内部<代码>$(document).ready(函数(){//将所有jQuery放在这里….})
这应该可以工作$(文档)。在('click','.remove',function()
尝试将jQuery放在DOM中ready。$(文档)。ready(function(){//Place all jQuery here…});
这应该可以工作$(文档)。在('click','remove',function()