除非在页脚中,否则jQuery不起作用

时间:2017-11-28 08:24:56

标签: javascript jquery html

我的jQuery字面上不起作用,除非它在我网站的HTML页脚中。为什么是这样?我之前从未遇到过这个问题,所以我不确定是什么导致了这个问题。 我的jQuery:

addressbook:
  image: addressbook
  depends_on: 
    - mongo
  environment:
    - MONGODB_DB_NAME=addressbook
  labels:
    - "traefik.backend=spring_boot"
    - "traefik.protocol=http"
    - "traefik.port=8080"
    - "traefik.frontend.entryPoints=http_8080"
  ports:
    - "8080"
  links:
    - mongo

web:
  image: docker-web-angularjs
  labels:
    - "traefik.backend=nginx"
    - "traefik.protocol=http"
    - "traefik.port=80"
    - "traefik.frontend.entryPoints=http_80"
  ports:
    - "80"

lb:
  image: traefik
  command: "--web --web.address=8081 --docker --docker.domain=docker.localhost \
            --logLevel=DEBUG \
            --entryPoints='Name:http_80 Address::80' \
            --entryPoints='Name:http_8080 Address::8080'"
  ports:
    - 80:80
    - 8080:8080
    - 8081:8081
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - /dev/null:/traefik.toml

由于

1 个答案:

答案 0 :(得分:1)

$(document).ready(function(){
    $('#trigger').click(function(e){
        var that = $(this);
        var wrap = that.parents('.wrap');
        that.toggleClass('open');
        $('.menu .sub-menu').removeClass('open');
        $('.menu').toggleClass('open');
        $(document.documentElement).toggleClass('menu-open');

        //console.log('clicked!');
        e.preventDefault();
    });

    var sub_parent = $(".sub-parent > a");
    var sub_menu = $('.sub-menu');
    sub_parent.click(function(){
        var that = $(this); //cache when you can
        var parent_menu = that.parents('.menu');
        var menu_index = parent_menu.index();
        var current_item = that.next('.sub-menu');
        //console.log(parent_menu);
        parent_menu.addClass('sub-level1-open');
        current_item.addClass('open');
    });

    var sub_back = $('.sub-menu--back');
    sub_back.click(function(){
        var that = $(this);
        var menuIndex = that.parent().index();
        var currentItem = that.parent('.sub-menu');
        var parent_menu = that.parents('.menu');
        currentItem.removeClass('open');
        parent_menu.removeClass('sub-level1-open');
    });
});

你必须用

包装你的代码
    $(document).ready(function(){ 
        // Your Code
    }); 

确保在访问DOM之前加载DOM。