简体   繁体   English

在动态元素上添加类

[英]Add class on dynamic elements

I have a class that when I add it it activates an animation with the use of ajax i'm creating list of topics after that i'm trying to add class for each li element with a delay so it looks like every second a new topic is added to the list, but it didn't work. 我有一个类,当我添加它时,它使用ajax激活动画我正在创建主题列表之后,我正在尝试为每个li元素添加一个延迟的类,所以看起来每秒都是一个新主题被添加到列表中,但它不起作用。

The css file include slideExpandUp class which does the animation (this work great) css文件包含执行动画的slideExpandUp类(这项工作很棒)

Here's the js code: 这是js代码:

$(document).ready(function(){
  GetTopics();
  AnimateEvent();
});


function GetTopics() {
  $.ajax({
    url: '/Main/GetTopics',
    dataType: "json",
    type: "POST",
    contentType: 'application/json; charset=utf-8',
    async: true,
    processData: false,
    cache: false,
    success: function (data) {
      if (data.topics != null) {
        var myTopics = "";

        $.each(data.topics, function (idx, obj) {
          myTopics += "<li class='topic'><a href='#'>" + obj.Topic + "</a></li>";
        });

        $("#topics").append(myTopics);
      }
      else {
        alert("2");
      }
    },
    error: function (xhr) {
        alert(xhr.responseText);
    }
  })
}

function AnimateEvent() {
  setTimeout(function () {
    $("#topics li").each(function () {
      $(this).addClass('slideExpandUp') 
    });
  }, 0);
}

To sum up, I need to know how, after creating elements, I'm adding class for each element with a delay between each add. 总而言之,我需要知道在创建元素之后,我是如何为每个元素添加类,每次添加之间都有延迟。

Thank you all. 谢谢你们。

You could use the setInterval() method instead : 您可以使用setInterval()方法:

var current_index=0;

setInterval(function () {
  activeNextLi(); 
}, 1000); //1 second

function activeNextLi(){
  if(current_index<$("#topics li").length-1)
    current_index++;
  else
    current_index=0;

  $("#topics li").removeClass('slideExpandUp');
  $("#topics li").eq(current_index).addClass('slideExpandUp')
}

That will add class slideExpandUp to all the li elements every second as the passed parameter 1000 shows. 这将在传递的参数1000显示时slideExpandUp向所有li元素添加类slideExpandUp

Hope this helps. 希望这可以帮助。

 var current_index=0; setInterval(function () { activeNextLi(); }, 1000); //1 second function activeNextLi(){ if(current_index<$("#topics li").length-1) current_index++; else current_index=0; $("#topics li").removeClass('slideExpandUp'); $("#topics li").eq(current_index).addClass('slideExpandUp') } 
 .slideExpandUp{ color:red; font-weight: bold; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id='topics'> <li class='slideExpandUp'>topic 1</li> <li>topic 2</li> <li>topic 3</li> <li>topic 4</li> </ul> 

try to make your event under AnimateEvent function as delegated event like this and use setInterval may be this can help you. 尝试将您的事件在AnimateEvent函数下作为委托事件,并使用setInterval这可以帮助您。

JAVASCRIPT CODE: JAVASCRIPT代码:

function AnimateEvent() {
  setInterval(function () {
    $('body').find("#topics li").each(function () {
      $(this).addClass('slideExpandUp') 
    });
  }, 100);
}

Here in your case the function AnimateEvent doesn't any effect whatsoever because function GetTopics is a asynchronous call and it will get finished after some delay in future. 在你的情况下,函数AnimateEvent没有任何影响,因为函数GetTopics是一个异步调用,它将在未来一段时间后完成。 While you are calling the AnimateEvent before anything added in the DOM. 在DOM中添加任何内容之前调用AnimateEvent

So, the solution is to pass the function as a callback and call it when you finish your append. 因此,解决方案是将函数作为回调传递,并在完成追加时调用它。

$(document).ready(function() {
  GetTopics(AnimateEvent); // <---pass the function as callback here.
});


function GetTopics(callback) {
  $.ajax({
    url: '/Main/GetTopics',
    dataType: "json",
    type: "POST",
    contentType: 'application/json; charset=utf-8',
    async: true,
    processData: false,
    cache: false,
    success: function(data) {
      if (data.topics != null) {
        var myTopics = "";

        $.each(data.topics, function(idx, obj) {
          myTopics += "<li class='topic'><a href='#'>" + obj.Topic + "</a></li>";
        });

        $("#topics").append(myTopics);
      } else {
        alert("2");
      }
      callback(); //<----call that function here.
    },
    error: function(xhr) {
      alert(xhr.responseText);
    }
  })
}

function AnimateEvent() {
  $("#topics li").each(function() {
    $(this).addClass('slideExpandUp')
  });
}

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

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