简体   繁体   English

元素异步加载后,如何将其添加到元素中?

[英]How can I add class to an element after it has loaded asynchronously?

My problem is when the element is loaded by AJAX and it's appended in the DOM during the success callback the addClass method executes too soon and the transitions are not displayed. 我的问题是,当元素由AJAX加载并且在成功回调过程中被添加到DOM中时,addClass方法执行得太早并且过渡不显示。

$.ajax({
    url : contentURL,
    type : "GET",
    dataType : "html",
    success : function(response){
        $content.append(response);
        $activeSection = $('#'+href);   // appended element
        $activeSection.addClass("open"); // class is added before appending
    }
});

Problem without ajax: demo 没有ajax的问题​​: 演示

Maybe you need to use some like: 也许您需要使用类似:

  setInterval(function(){
    if($('#elementId').html() != null) {
      $('#elementId').addClass('.class');
      return;
    }
    console.log('Working...');
    }, 1000);

This is run antil the element exist and class has been adedded. 这是在元素存在且类已添加的情况下运行的。 (sorry for bad english) (对不起,英语不好)

You should try sync ajax call ie async: false 您应该尝试同步ajax调用,即async:false

refer following link for more detail http://api.jquery.com/jquery.ajax/ 请参阅以下链接以获取更多详细信息http://api.jquery.com/jquery.ajax/

Manipulating the DOM is really resource intensive. 操作DOM确实会占用大量资源。 While a new element is under construction it was given it's class attribute. 在构造一个新元素时,为其提供了class属性。 My solution is preparing a wrapper element before manipulating the DOM, and modifying the class attribute only on the wrapper. 我的解决方案是在操作DOM之前准备包装器元素,并仅在包装器上修改class属性。 Right after the new element has been created I have inserted a new empty wrapper for further content loading. 在创建新元素之后,我立即插入了一个新的空包装器以进一步加载内容。

my solution 我的解决方案

function addClass(el, classN) {
  var hasClass = el.className.search(classN)+1;
  if (hasClass) {
      return true
  } else {
      el.className += el.className ? " "+classN : classN;
  }
}
function removeClass(el, classN) {
  var re = new RegExp("\\s*"+classN, "ig");
    el.className = el.className.replace(re, "");
    if (el.className.length === 0) {
        el.removeAttribute("class");
    }
}
function dropping (el, classN, duration){
    addClass(el, classN);
  setTimeout(function(){
    el.parentNode.removeChild(el);
    state2 = null;
  }, duration);
}
function opening(e) {
    if (!open) {
    open = true;
    addClass(w1, "open");
    addClass(w2, "open");
    addClass(state0, "open");
    state1 = state0;
    state0 = conClear.cloneNode(true);
    w3.appendChild(state0);
  }
}
function closing(e) {
    if (open) {
    open = false;
    removeClass(w1, "open");
    removeClass(w2, "open");
    dropping(state1, "drop", 1000);
    state2 = state1;
    state1 = null;
  }
}
function paging(e){
    if (open) {
    dropping(state1, "drop", 1000);
    state2 = state1;
    addClass(state0, "open");
    state1 = state0;
    state0 = conClear.cloneNode(true);
    w3.appendChild(state0);
  }
}
var open = false;
var bO = document.getElementById("open");
var bC = document.getElementById("close");
var bP = document.getElementById("page");

var w1 = document.getElementById("wrapper1");
var w2 = document.getElementById("wrapper2");
var w3 = document.getElementById("wrapper3");
var con = w3.getElementsByClassName("content").item(0);
var state0 = con;
var state1 = null;
var state2 = null;
var conClear = con.cloneNode(true);

bO.addEventListener("click", opening, false);
bC.addEventListener("click", closing, false);
bP.addEventListener("click", paging, false);

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

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