简体   繁体   English

动态生成的元素内的jQuery无法运行

[英]JQuery inside dynamically generated element doesn't run

I have an index which shows a list of orders, each of wich calls a function (named dinamically with php when i brought the data from the db), to simplify i've reduced the function that each div contains to just an alert. 我有一个显示命令列表的索引,每个命令都调用一个函数(当我从数据库中获取数据时,用php命名),为简化起见,我已将每个div包含的函数简化为一个警报。 But also every minute an ajax function executes that searches for new orders and appends them on top, with the exact same code as the ones initially loaded. 而且每分钟都会执行一个ajax函数,该函数搜索新订单并将其追加到顶部,并使用与最初加载的代码完全相同的代码。 The jquery works perfectly in the elements that are loaded initially but doesn't work at all in the elements generated dinamically. jQuery在最初加载的元素中运行完美,但在动态生成的元素中根本不起作用。

This is the index with one initial order inside, BEFORE newOrders runs for the first time. 这是内部有一个初始订单的索引,在newOrders首次运行之前。 The alert on that order functions properly 该订单的警报正常运行

<div id="content">
  <div id="pedido_4126" class="pedido">
    <h4>Pedido 4126</h4>
    <button id="btn4126">Alert</button>
    <script>    
      alert("Pedido 4126");
    </script>
  </div>
</div>
<script>
  function newOrders() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "simplereq.php", true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
       var response = JSON.parse(this.responseText);
       console.log(response);
      var element = document.querySelector('#content');
      var content = element.innerHTML;
      ultimoid = response.ultimoid;
      element.innerHTML = response.contenido + content;
      }
    };
    xhttp.send("ultimoid="+encodeURIComponent(ultimoid));
  }
  setInterval(newOrders, 60000);
</script>

And this is the index when the function has executed once and appended a new order on top with it's corresponding script, dynamically generated and received from the AJAX call: 这是函数执行一次并在其相应脚本的顶部附加新顺序(动态生成并从AJAX调用接收)时的索引:

<div id="content">
  <div id="pedido_4255" class="pedido">
    <h4>Pedido 4255</h4>
    <button id="btn4255">Alert</button>
    <script>
      alert("Pedido 4255");
    </script>
  </div>
  <div id="pedido_4126" class="pedido">
    <h4>Pedido 4126</h4>
    <button id="btn4126">Alert</button>
    <script>
      alert("Pedido 4126");
    </script>
  </div>
</div>
<script>
  function newOrders() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "simplereq.php", true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
       var response = JSON.parse(this.responseText);
       console.log(response);
      var element = document.querySelector('#content');
      var content = element.innerHTML;
      ultimoid = response.ultimoid;
      element.innerHTML = response.contenido + content;
      }
    };
    xhttp.send("ultimoid="+encodeURIComponent(ultimoid));
  }
  setInterval(newOrders, 60000);
</script>

As you can see, the html and script are exactly the same, but the one on the new order brought by the ajax call, doesn't work. 如您所见,html和脚本完全相同,但是由ajax调用带来的新订单上的html和脚本不起作用。

Ok, so doing more research I came upon the best answer for my case, I'll leave it here in case it helps someone: 好的,因此,进行更多研究后,我找到了适合自己情况的最佳答案,如果有帮助,我将在此处保留:

In the content I generate in the AJAX call, I print the scripts like this, and obviously hide it with css: 在我通过AJAX调用生成的内容中,我将打印如下脚本,并显然使用CSS将其隐藏:

<div class="javascript">
     $("body").on("click","#btn4255",function(){
      alert("Pedido 4255");
    });
    </div>

And then I execute this function every time the AJAX call is returned 然后,每次返回AJAX调用时,我都会执行此函数

$('.javascript').each(function() {
      eval($(this).text());
    });

I only evaluate strings I generate myself so in this case I think it's not unsafe to use eval(). 我只评估自己生成的字符串,因此在这种情况下,我认为使用eval()并非不安全。

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

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