简体   繁体   English

$('。photos ul li')。length在我注入照片之前输出0,当它们出现时输出5,但是当我删除它们时仍然显示5

[英]$('.photos ul li').length outputs 0 before i inject the photos and outputs five when they appear but when i remove them it still shows 5

when i click on the removePhotos button it will output the photos li length. 当我单击removePhotos按钮时,它将输出照片长度。 initially when the DOM loads their are no photos so it would output 0 and when i click show photos i use ajax to inject my photos. 最初,当DOM加载时,它们没有照片,因此它将输出0;当我单击“显示照片”时,我使用ajax注入照片。 after i insert my photos and click the removePhotos button it outputs 5 which is the number of photos i have but the problem is when i remove the photos and then click the removePhotos button it outputs 5 still when it should be outputting 0. any suggestions on how to fix this? 在我插入照片并单击removePhotos按钮后,它输出5,这是我所拥有的照片数量,但问题是当我删除照片时,然后单击removePhotos按钮,当它应输出0时仍输出5。如何解决这个问题?

my html 我的HTML

    <div id="paris">
     <h1>CRAPPY CHEESE</h1>
     <button>Get Paris Photos</button>
     <span>Remove Photos</span>
     <ul class="photos">
     </ul>
    </div>

my javascript 我的JavaScript

    function Tour(el) {
      var tour = this;
      this.el = el;

     this.hidePhotos = function() {
      alert($('.photos ul li').length);
      $('.photos').fadeOut();
     }

     this.fetchPhotos = function() {
       $.ajax('photos.html', {
          data: {location: $("#tour").data(this.el)},
          success: function(response) {
           this.el.find('.photos').html(response).fadeIn();
          },
          context: tour,
          error: function() {
            this.el.find('.photos').html('<li>There was a problem fetching the latest photos. Please try again.</li>');
          },
          timeout: 3000,
          beforeSend: function() {
            this.el.addClass('is-fetching');
          },
          complete: function() {
            this.el.removeClass('is-fetching');
          }
        });
      };
   var kirt = this.el.on('click.showPhotos', 'button', this.fetchPhotos);
   var pooh = this.el.on('click.removePhotos', 'span', this.hidePhotos);
  }
  $(document).ready(function() { 
     var paris = new Tour($('#paris'));
  });

Pass into the jQuery fadeOut function a callback which does a $(this).remove() 将执行$(this).remove()的回调传递到jQuery fadeOut函数中

Your problem is, that fadeOut just hides your photos, but they are still present in the DOM. 您的问题是,fadeOut只会隐藏您的照片,但是它们仍然存在于DOM中。

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

相关问题 当我尝试对它们进行排序时,如何在javascript中获取当前ul li的长度? - How to get the current ul li's length in javascript , when i tried to sort them? 如何让我的照片幻灯片在页面打开时出现,而不是在我单击点 HTML CSS JavaScript 时出现? - How do I make my slideshow of Photos appear when the page opens, instead of when I click the dots, HTML CSS JavaScript? 使用Firebug时,部署时是否必须删除控制台输出? - When using firebug, do I have to remove the console outputs when deploying? 单击时如何更改页面上的所有照片 - how do i change all photos on a page when one is clicked 当我裁剪照片时,它们最终将标题隐藏在缩略图中 - When i crop the photos, they end up hiding the captions within the thumbnail 在 Carousal 图像 slider 中,照片仅在单击点时出现 - In Carousal image slider, photos appear only when clicked on the dot 照片在显示时重复 - Photos duplicating when displayed 当我在最后使用die()时,ajax函数输出“ 0” - ajax function outputs a “0” when I use die() at the end 当我点击li时为什么会触发ul? - Why ul gets triggered when I click on li? 当我使用 React 单击下拉项目时如何显示照片 - How can I display photos when I click dropdown items with React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM