2017-04-17 61 views
-1

我有jQuery代码不工作没有提示语句。 整个JavaScript文件是
codepen.io/smarty_tech/pen/gWpKjEjQuery不工作没有警报

(function($) { 
    jQuery.fn.lightTabs = function(options) { 
    var createTabs = function() { 
     tabs = this; 
     i = 0; 

     showPage = function(i) { 
     $(tabs).children("div").children("div").hide(); 
     $(tabs).children("div").children("div").eq(i).show(); 
     $(tabs).children("select").children("option").removeClass("current"); 
     $(tabs).children("select").children("option").eq(i).addClass("current"); 
     } 

     showPage2 = function(i) { 
     $(tabs).children("div").children("div").hide(); 
     $(tabs).children("div").children("div").eq(i).show(); 
     clasw = '.re-compare-wrap-' + i; 
     $(clasw).css("display", "block"); 
     $(clasw).css("display", "block"); 
     } 

     showPage(0); 

     datap = $('#re-compare-bar-tabs').children("select").children("option:selected").attr("data-page"); 

     <!-- $(tabs).children("select").children("option").removeClass("current"); --> 

     $('.re-compare-wrap').css("display", "none"); 

     clas = '.re-compare-wrap-' + datap; 
     $(clas).css("display", "block"); 

     function ret(dt) { 
     return dt.attr('data-comparing'); 
     } 

     function abc() { 
     var dt = $("#re-compare-bar-tabs select option:first"); 
     va1 = ret(dt); 
     alert(va1); 
     if (typeof va1 !== typeof undefined && va1 !== false) { 
      alert('hi'); 
      words1 = va1.split(","); 
      text1 = $(this).html(); 
      pos11 = text1.indexOf("("); 
      pos21 = text1.indexOf(")"); 
      $(this).text(text1.substring(0, pos11 + 1) + words1.length + text1.substring(pos21)); 

     } 

     $("#re-compare-bar-tabs select > option").each(function() { 

      va = ret($(this)); 
      alert(va); 
      if (typeof va !== typeof undefined && va !== false) { 

      words = va.split(","); 
      alert(words.length); 
      text = $(this).html(); 
      pos1 = text.indexOf("("); 
      pos2 = text.indexOf(")"); 
      $(this).text(text.substring(0, pos1 + 1) + words.length + text.substring(pos2)); 

      } 
     }); 
     } 

     abc(); 

     $(tabs).children("select").children("option").each(function(index, element) { 
     $(element).attr("data-id", i); 
     i++; 
     }); 
     $(tabs).children("select").change(function() { 

     var element = $(this).find('option:selected'); 
     showPage(parseInt(element.attr("data-id"))); 
     }); 
    }; 
    return this.each(createTabs); 
    }; 
})(jQuery); 

从功能退役以后的代码不无警示工作。我试过https://www.sitepoint.com/community/t/script-works-only-when-alert-is-called/2803/7,但没有运气。

+0

把你的函数'ret()'和函数'abc()'放在'(function($){....})'之外'。如果没有在jQuery.fn.lightTabs = function(options)中调用 –

+0

{那么它不起作用 – smarttechy

+0

将两个函数放在外面并在'(function($){....})中调用它。 ' –

回答

0

您可以使用setTimeout来创建延迟而不是警报。

setTimeout(function(){ 
your code here 
}, 1000); 
+0

没有它的不工作 – smarttechy