最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does some jquery code only work if it's at the end of my html-body section? - Stack Overflow

programmeradmin0浏览0评论

I have the following code for creating tabs. It works at the end of the html body section but not if I place it at the beginning - before all of the divs are defined. Why might that be?

<script type="text/javascript">
    $("ul.tabs li.label").hide();
    $("#tab-set > div").hide();
    $("#tab-set > div").eq(0).show();
  $("ul.tabs a").click(
    function() {
        $("ul.tabs a.selected").removeClass('selected');
        $("#tab-set > div").hide();
        $(""+$(this).attr("href")).show();
        $(this).addClass('selected');
        return false;
    }
  );
  $("#toggle-label").click( function() {
    $(".tabs li.label").toggle();
    return false;
  });
</script>

I have the following code for creating tabs. It works at the end of the html body section but not if I place it at the beginning - before all of the divs are defined. Why might that be?

<script type="text/javascript">
    $("ul.tabs li.label").hide();
    $("#tab-set > div").hide();
    $("#tab-set > div").eq(0).show();
  $("ul.tabs a").click(
    function() {
        $("ul.tabs a.selected").removeClass('selected');
        $("#tab-set > div").hide();
        $(""+$(this).attr("href")).show();
        $(this).addClass('selected');
        return false;
    }
  );
  $("#toggle-label").click( function() {
    $(".tabs li.label").toggle();
    return false;
  });
</script>
Share Improve this question edited Jul 7, 2009 at 15:55 anonymous coward 12.8k13 gold badges58 silver badges99 bronze badges asked Jul 7, 2009 at 15:46 ipso factoipso facto 4452 gold badges7 silver badges11 bronze badges 1
  • "...before all of the divs are defined" should give you a clue - how can your code possibly manipulate them if they haven't been defined yet? – NickFitz Commented Jul 7, 2009 at 16:08
Add a ment  | 

3 Answers 3

Reset to default 10

It is most likely because the DOM is not ready yet, and therefore they don't exist.

Therefore you need to do the following:

$(function() {
    // Any code in here will only be executed when the DOM is ready.
});

you need to wrap it with a document ready block. this prevents the code firing until the page is fully loaded.

<script type="text/javascript">
    $(function() {
      // do something on document ready
      $("ul.tabs li.label").hide();
      $("#tab-set > div").hide();
      $("#tab-set > div").eq(0).show();
      $("ul.tabs a").click(
        function() {
          $("ul.tabs a.selected").removeClass('selected');
          $("#tab-set > div").hide();
          $(""+$(this).attr("href")).show();
          $(this).addClass('selected');
          return false;
        }
      );
      $("#toggle-label").click( function() {
        $(".tabs li.label").toggle();
        return false;
      });
    });
</script>
jQuery(function($) {
    // put your code in here and it will be executed
    // when the document has fully loaded.
});
发布评论

评论列表(0)

  1. 暂无评论