Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript on()不';我认不出一个班级_Javascript_Jquery_Html - Fatal编程技术网

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()