简体   繁体   English

ajax xml加载后jQuery切换不起作用

[英]jQuery toggle doesn't work after ajax xml load

I can't seem to figure out why the toggle doesn't work after my ajax xml load. 我似乎无法弄清楚为什么我的ajax xml加载后切换不起作用。 It pulls the xml correctly. 它可以正确提取xml。 However, fails to execute the toggle. 但是,无法执行切换。

Here is the jQuery onclick both xml load and toggle 这是jQuery onclick xml加载和切换

      $("document").ready(function(){

           $("#demo").on("click", loadxml);
           $("#demo-2").click(function(){
              $("li").toggle("fast");        
          });
        });

The loadxml function : loadxml函数:

    function loadxml(){
     var xhttp = new XMLHttpRequest();
     xhttp.onreadystatechange = function(){
         if (xhttp.readyState == 4 && xhttp.status == 200) {
             myFunction (xhttp);
          }
    };
    xhttp.open("GET","xml/categories.xml", true);
    xhttp.send();
}

 function myFunction(xml){
      var i;
      var xmlDoc = xml.responseXML;
      var list = '<ul id="demo-2" class="list-unstyled no-bullet"><span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span> Categories';
     var x = xmlDoc.getElementsByTagName("Categories");

  for(i=0; i<x.length; i++) {
        list += '<li><span class="glyphicon glyphicon-minus" aria-hidden="true"></span>' +  x[i].getElementsByTagName("CategoryName")[0].childNodes[0].nodeValue + "</li>";
}

list += "</ul>"

document.getElementById("change").innerHTML = list;

} }

Here is the html: 这是html:

        <div class="container" id="change">
             <ul id="demo"></ul>
         </div>

This code: 这段代码:

$("#demo-2").click(function(){
   $("li").toggle("fast");        
});

Should be: 应该:

$("#change").on('click','#demo-2',function(){
   $("li").toggle("fast");        
});

Thats because your are adding dinamic content on your #change selector. 那是因为您要在#change选择器上添加动态内容。

I'm pretty sure it's not working because of when you are binding the click event. 我非常确定由于您绑定click事件时,它不起作用。

If you're sending the xmlhttp request after your document ready event is fired, you've already bound the click event to '#demo-2', which is not there yet. 如果您在触发文档就绪事件后发送xmlhttp请求,那么您已经将click事件绑定到了“#demo-2”(尚不存在)。 Im pretty sure you need to defer the binding until after you insert the element into the dom. 我非常确定您需要将绑定推迟到将元素插入dom之后。

No problem. 没问题。 If Hackerman's solution works, it's more elegant, and honestly, correct, than what I was thinking. 如果Hackerman的解决方案有效,那么它比我在想的要优雅,诚实,正确。 But, if not: I was thinking that you should wait to bind until after 但是,如果没有:我在想你应该等到绑定之后再绑定

document.getElementById("change").innerHTML = list;

That way the content is loaded into the dom, and then the click event is bound. 这样,将内容加载到dom中,然后绑定click事件。

// Just for simplicity, I'm pretending your xml is returned from the function
var xml = loadxml();
myFunction(xml);
// Now, bind your click event to the new element

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

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