简体   繁体   English

jQuery动态添加的类不起作用

[英]Jquery Dynamically added class Not working

I am stucked in my Timerapp with jquery. 我被jquery卡在我的Timerapp中。 Below is my scenario, 以下是我的情况,

  1. One Button has class named toStart 一个按钮的类名为toStart
  2. When click on it an ajax function will storing current time in database.(ajax function not in stack-code). 单击它时,一个ajax函数将当前时间存储在数据库中。(ajax函数不在堆栈代码中)。

3.after successful ajax response the button class changed to pause. 3.ajax响应成功后,按钮类更改为暂停。 now the class name of the button is pause. 现在,按钮的类名已暂停。

4.After some time, when Click on the Button again (class named:pause), i want to call a ajax function with current time of click and store the values in database. 4.一段时间后,当再次单击Button(名为:pause的类)时,我想用当前的单击时间调用一个ajax函数,并将值存储在数据库中。

5.Then the Button class name will changed to old ( toStart). 5.然后,Button类名称将更改为old(toStart)。

HTML : HTML:

<button class="toStart" value="1" va>Start</button>

JS: JS:

$('.toStart').click(function()  
{
   //ajax function { addStart}
   $("button[value=1]").removeclass('toStart');
   $("button[value=1]").addClass('pause');
}

$(document.body).on('click','.pause',function() 
{
  //ajax function {addStop}
  $("button[value=1]").addClass('toStart');
}

This process repeating for every click function simultaneously. 每次单击功能都会同时重复此过程。

on body loads when i click on the button it goes to the ajax function and added class (pause) into the button (working fine). 当我单击按钮时,它会加载ajax函数并将类(暂停)添加到按钮中(工作正常)。

But when i click on the button (class name: pause) the toStart function working and after that the pause function working in same time. 但是,当我单击按钮(类名:pause)时,toStart函数开始工作,然后pause函数同时开始工作。

How can i solve this issues.. 我该如何解决这个问题。

Attaching my Webdeveloper->Network Graph. 附加我的Webdeveloper->网络图。 You can see the addStart Ajax performing twice. 您可以看到addStart Ajax执行两次。

在此处输入图片说明

You need event delegation on the first listener as well: 您还需要在第一个侦听器上进行事件委派:

$(document.body).on('click', '.toStart', function()  
{
   //ajax function { addStart}
   $("button[value=1]").removeclass('toStart');
   $("button[value=1]").addClass('pause');
}

Why? 为什么? Because the way you did it added the listener on all .toStart elements. 因为您的方式是在所有.toStart元素上添加了侦听器。 Removing that class will not remove the listener. 删除该类不会删除侦听器。

I would do it this way as it's cleaner and readable. 我会这样做,因为它更清晰易读。 Demo@ fiddle 演示@ 小提琴

$(document).on("click", ".control", function() {
    var $this = $(this);
    //ajax function { addStart}
    if ( $this.is(".toStart") ) {
        $this.removeClass('toStart').addClass('pause');
        $this.text("Start"); //for demo only
        //Code to be executed when paused
    } else {
        $this.addClass('toStart').removeClass('pause');
        $this.text("Pause"); //for demo only
        //Code to be executed when played/started
    }
});

HTML: HTML:

<button class="control pause" value="1" va>Start</button>
<!--
OR
<button class="control toStart" value="1" va>Pause</button>
-->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM