簡體   English   中英

找不到子元素

[英]Can't find children of element

由於某種原因,我無法從類.slideshow-dots的div中獲取孩子。 這是有孩子的div:

<div class="slideshow-dots">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>

這是錯誤:

未捕獲的TypeError:無法讀取未定義的屬性'className'

下面是我的代碼,我也無法調用公共方法showSlides

(function($) {
    $.fn.MarvSimpleGallery = function( options ) {
      var instance = this;
      instance.index = 1;

      var settings = $.extend({
        // These are the defaults.
        arrows: true,
        dots: true,
        numbers: true
      }, options );

      var init = function() {
        instance.dotCont = instance.find('.slideshow-dots')[0];
        instance.slides = instance.find('.mySlides');

        // Assign event listeners
        instance.find('.prev').click(function() { instance.showSlides(instance.index += -1); });
        instance.find('.next').click(function() { instance.showSlides(instance.index += 1); });

        // Initiate dot controls
        $.each(instance.slides, function(index, data) {
          var dot = $('<span></span>');
          dot.addClass('dot');
          dot.click(function() { instance.showSlides(instance.index = (index + 1)); });

          $(instance.dotCont).append(dot); 
        });

        // Show initial slide


      }();

      instance.showSlides = function(n) {
        console.log('Slide: ' + n);
        if (n > (instance.slides).length) instance.index = 1;
        if (n < 1) instance.index = (instance.slides).length;

        var slideCount = (instance.slides).length;

        $.each(instance.slides, function(index, data) {
          data.style.display = "none";
          $(data).prepend($('<div></div>').addClass('numbertext').append((index + 1) + ' / ' + slideCount));
        });

        instance.slides[instance.index-1].style.display = "block"; 
        $(instance.dotCont).find('dots')[instance.index-1].className += " active";
      };

    }
  }(jQuery));

嘗試

$(instance.dotCont).find('dot')[instance.index-1].className += " active";

代替

$(instance.dotCont).find('dots')[instance.index-1].className += " active";

您的子元素具有類“點”而不是“點”。

編輯 -如評論中所述:

我忘了句號:最后一句話必須是

$(instance.dotCont).find('.dot')[instance.index-1].className += " active";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM