简体   繁体   English

Ajax调用后Javascript中断

[英]Javascript breaks after ajax call

I have a list of items. 我有项目清单。 When one is clicked, I use javascript to show a dropdown of more information. 当单击一个时,我使用javascript来显示更多信息的下拉列表。

The javascript works for the initial items, but when I dynamically load new items on this page using ajax, it stops. javascript适用于初始项目,但是当我使用ajax在此页面上动态加载新项目时,它将停止。

At the moment, the Javascript is placed in the footer. 目前,JavaScript已放置在页脚中。 I've tried placing it in different places in the code but to no avail. 我尝试将其放置在代码中的不同位置,但无济于事。 No errors show up in the console either. 控制台中也不会显示任何错误。

I'm following the w3schools accordion tutorial here ( https://www.w3schools.com/howto/howto_js_accordion.asp ), and I'm using infinte scroll from Meta Fizzy ( https://infinite-scroll.com/ ) 我在这里关注w3schools手风琴教程( https://www.w3schools.com/howto/howto_js_accordion.asp ),并且正在使用Meta Fizzy的无限滚动( https://infinite-scroll.com/

HTML HTML

<ul class="item-listings">
<li class="item">
<button class="accordion">Section 1</button>
<div class="panel">
  <p>Lorem ipsum...</p>
</div>
</li>

Javascript 使用Javascript

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active");
    this.parentNode.classList.toggle("active");

    var panel = this.nextElementSibling;
    if (panel.style.maxHeight){
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    } 
  });
}

Ajax 阿贾克斯

<script>
jQuery(function(a){a("ul.item-listings").infiniteScroll({path:".next",append:"li.item",button:".view-more-button",scrollThreshold:!1,loadOnScroll:!1,hideNav:".pagination",history:!1,status:".page-load-status",checkLastPage:!0});var b=a(".view-more-button");a=a("ul.item-listings");var c=!1;a.on("request.infiniteScroll",function(a,c){b.hide()});a.on("load.infiniteScroll",function(a,d,e,f){c||b.show()});a.on("last.infiniteScroll",function(a,d,e){b.hide();c=!0})});
</script>

I'm not really sure what to do. 我不太确定该怎么办。 Is there a way to reload this everytime I make the ajax call, or could my javascript be changed in some way? 每当我进行ajax调用时,是否可以重新加载此代码,或者可以以某种方式更改我的JavaScript? I thought that because it used a click event listener it would be fine, but sadly not. 我以为,因为它使用了单击事件侦听器,所以很好,但可惜不是。

You add event listeners to inital items only, but not on new items after they're added. 您只能将事件侦听器添加到初始项,而不能将它们添加到新项。

You need event propagation, basically you attach event listener to higher element (for example window, body or container of all items) and then on that event listener you check event target's className or whatever. 您需要事件传播,基本上是将事件侦听器附加到较高的元素(例如,所有项目的窗口,主体或容器),然后在该事件侦听器上检查事件目标的className或其他内容。

Since you are using jQuery, you can use jQuery's way to do that with using .on 由于您使用的是jQuery,因此可以使用jQuery的方法来使用.on

So here is how to do it with jQuery, if you wish I can rewrite in plain JS. 如果您希望我可以用普通的JS重写,那么这就是使用jQuery的方法。

$('.accordion').on('click', function() {
  $(this).toggleClass('active');
  $(this).parent().toggleClass('active');
  const panel = $(this).next()
  if (panel.css('maxHeight')) {
    panel.css({ maxHeight: 'null'})
  } else {
    panel.css({ maxHeight: panel[0].scrollHeight + 'px' })
  } 
});

There is probably way to get scrollHeight in jquery way but I havent done jQuery for long time, just cant remember. 有可能以jquery的方式获取scrollHeight的方法,但是我已经很长时间没有做jQuery了,只是记不清了。

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

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