jQuery" on"事件委托不使用AJAX调用

时间:2015-11-16 14:59:58

标签: javascript jquery html ajax

我遇到了jQuery的on事件委派问题:

这是我的代码:

HTML

<body>      
  <!-- Page Content Wrapper-->        
  <div id="sb-site">
     <div id="overlay">&nbsp;</div>

     <!-- Ajax loaded here -->
     <div id="ajax-loadHeader" class="homeNavbar">
     </div>

  </div>
  <!-- End Page Content Wrapper --> 
</body>

JS

$.get("header.html").done(function (data) {
    var html = $(data);
    var transition = $("#ajax-loadHeader").hasClass("homeNavbar");
    if(transition){
        html = html.attr("data-transitions","true").addClass("home");
    }
    $("#ajax-loadHeader").append(html);
    $.slidebars({
        siteClose: true, // true or false
        disableOver: 767, // integer or false
        scrollLock: true // true or false
    }); 
});

问题在于以下功能。单击按钮时,没有一个被触发。但是,在jQuery 1.7之后,$(document).on("click","selector",function(){});甚至可以用于动态加载的元素。

我也尝试过:$("selector").on("click",function(){});

//The element is present on header.html.
$(document).on("click","#mobile-hamburger-menu-open",function(){
    $("#overlay").toggleClass("overlay");
    $("body").toggleClass("overlayed");
    $("#mobile-hamburger-menu-close").removeClass("hide");
    $("#mobile-hamburger-menu-open").addClass("hide");
    $("#sb-site").addClass("locked");
});

//The element is present on header.html.
$(document).on("click","#mobile-hamburger-menu-close",function(){
    $("#overlay").toggleClass("overlay");
    $("body").toggleClass("overlayed");
    $("#mobile-hamburger-menu-close").addClass("hide");
    $("#mobile-hamburger-menu-open").removeClass("hide");
    $("#sb-site").removeClass("locked");
});

//The #navbar-top element is present on header.html.
$(document).on("click","#overlay, #navbar-top nav[class*='navbar-default']",function(){
    if($("#overlay").hasClass("overlay")){
        $("#overlay").removeClass("overlay");
        $("body").removeClass("overlayed");
        $("#mobile-hamburger-menu-close").addClass("hide");
        $("#mobile-hamburger-menu-open").removeClass("hide");
        $("#sb-site").removeClass("locked");
    }
});

在header.html中显示HTML

<div id="login-section" class="login-section closed container-fluid">
              <div class="row">
                 <div class="col-md-8 col-sm-6 hidden-xs banner-login">
                    <div class="row">
                       <div class="col-md-6 col-sm-12 col-md-offset-6 col-sm-offset-0">
                          <div class="banner-login-text">
                             <div class="banner-login-title">
                                text
                             </div>
                             <p>
                                text
                             </p>
                          </div>
                       </div>
                    </div>
                 </div>
                 <div class="col-md-4 col-sm-6 col-xs-12 no-gutters">
                    <!-- Login Content -->
                    <div class="row no-gutters">
                       <div class="col-md-6 col-sm-6 col-xs-6">
                          <div id="sou-cliente-btn" class="tab-btn tab-active">
                             text
                          </div>
                          <div class="arrow-tab"></div>
                       </div>
                       <div class="col-md-6 col-sm-6 col-xs-6">
                          <div id="nao-sou-cliente-btn" class="tab-btn">
                             text
                          </div>
                          <div class="arrow-tab"></div>
                       </div>
                    </div>
                    <div class="login-content">
                       <div class="login-form">
                        <form id="form-validate-login">
                          <div class="row">
                             <div class="col-md-12">
                                <div class="form-group">
                                   <label class="col-md-3 col-sm-4">E-mail</label>
                                   <div class="col-md-9 col-sm-8">
                                      <input id="usernameField" type="text" data-rule-required="true" class="login-input" placeholder="nome@example.com">
                                   </div>
                                </div>
                             </div>
                          </div>
                          <div class="row">
                             <div class="col-md-12">
                                <div class="form-group">
                                   <label class="col-md-3 col-sm-4">Senha</label>
                                   <div class="col-md-9 col-sm-8">
                                        <input id="passwordField" data-inputmask-regex="[a-zA-Z0-9]{8,15}" data-rule-minlength="8" data-rule-maxlength="15" type="password" data-rule-required="true" class="login-input required" placeholder="Digite sua senha">
                                   </div>
                                </div>
                             </div>
                          </div>
                          <div class="row">
                             <div class="col-md-12">
                                <button type="submit" class="btn btn-primary"> entrar </button>
                             </div>
                          </div>
                        </form>
                          <div class="row">
                             <div class="col-md-12 col-xs-12"> <a class="link-arrow">Esqueci meus dados</a> </div>
                          </div>
                          <div class="row no-login">
                             <div class="col-md-12 col-xs-12"> <span>Se você não possui login:</span> </div>
                             <div class="col-md-12">
                                <a class="btn btn-style2"> crie seu primeiro cadastro </a>
                             </div>
                          </div>
                       </div>
                       <div class="novo-cadastro-section hide">
                          <span class="nao-cliente-title">text</span>
                          <br/>
                          <p class="nao-cliente-subtitle">text</p>
                          <a class="btn btn-primary">text</a>
                       </div>
                    </div>
                    <!-- End Login Content -->
                    <div class="row area-login login-especial-input hide">
                       <div class="col-md-12 col-xs-12">
                          <select class="col-md-12 col-xs-12 padding15">
                             <option selected disabled="disabled">Estabelecimento</option>
                          </select>
                       </div>
                    </div>
                    <div class="col-md-8 nao-cadastrado-section-container hide">
                       <div class="hidden-xs nao-cadastrado-section">
                          <div class="row">
                             <div class="col-md-12"> <span class="nao-cadastrado-title">Não é cadastrado?</span> </div>
                          </div>
                          <div class="row nao-cadastrado-description">
                             <div class="col-md-12"> <span class="">text</span> </div>
                          </div>
                          <div class="row nao-cadastrado-btns">
                             <div class="col-md-6">
                                <div class="row area-login">
                                   <div class="col-md-12 col-xs-12"> <strong><span class="control-label">Já sou cliente</span></strong> </div>
                                </div>
                                <div class="row area-login">
                                   <div class="col-md-12">
                                      <button type="submit" class="btn col-md-12 col-xs-12 primeiro-cadastro-btn"> Criar meu primeiro acesso </button>
                                   </div>
                                </div>
                             </div>
                             <div class="col-md-6">
                                <div class="row area-login">
                                   <div class="col-md-12 col-xs-12"> <strong><span class="control-label">Não sou cliente</span></strong> </div>
                                </div>
                                <div class="row area-login">
                                   <div class="col-md-12">
                                      <button type="submit" class="btn col-md-12 col-xs-12 primeiro-cadastro-btn"> Quero testar o gerenciador</button>
                                   </div>
                                </div>
                             </div>
                          </div>
                       </div>
                    </div>
                    <div class="row area-login login-especial-input hide">
                       <div class="col-md-12">
                          <div class="input-group"> <span class="input-group-addon login-description"> Código </span>
                             <input type="text" class="login-input" placeholder="XXXXXXXXXXXX">
                          </div>
                       </div>
                    </div>
                 </div>
                 <div class="primeiro-cadastro-section"> </div>
              </div>
           </div>
           <!-- End Login -->
     <!-- Header -->
      <header id="navbar-top" data-transitions="false">             
           <div class="container-fluid over-video-container">
              <nav class="navbar navbar-default over-video-navbar">
                 <div class="container">
                    <!-- Nav Left -->
                    <div class="col-md-1 col-sm-2 col-xs-7 col-brand">
                       <div class="navbar-header">
                          <!-- Menu hamburger -->
                          <button id="mobile-hamburger-menu-open" type="button" class="mobile-sidebar-menu navbar-toggle collapsed is-closed sb-open-left" aria-expanded="false" aria-controls="navbar">
                             <div class="label-hamburger"></div>
                             <div class="icon"></div>
                          </button>
                          <button id="mobile-hamburger-menu-close" type="button" class="mobile-sidebar-menu navbar-toggle collapsed is-closed sb-close hide" aria-expanded="false" aria-controls="navbar">
                             <div class="label-hamburger"></div>
                             <div class="icon"></div>
                          </button>
                          <!-- End Menu hamburger -->
                          <!-- Logo -->
                          <a class="navbar-brand" href="index.html" itemprop="url">
                             <img class="logo-svg" src="images/logo_rede.svg" itemprop="logo">
                             <!-- Logo PNG for IE8 -->
                             <img class="logo-png" src="images/logo_rede.png" >
                          </a>
                          <!-- End Logo -->
                       </div>
                    </div>
                    <!-- End Nav Left -->
                    <div class="col-md-4 col-sm-4 col-xs-5 pull-right nav-right-container">
                       <!-- Nav Right -->
                       <ul class="nav navbar-nav navbar-right">
                          <li class="col-md-4 col-md-offset-1 col-sm-5 help col-xs-6">
                             <!-- AJUDA <a href="#">Ajuda</a> -->
                          </li>
                          <li id="my-account-section" class="my-account col-md-7 col-sm-7 col-xs-6"> <a role="button">Minha Conta<span class="arrow hidden-xs hidden-sm"></span></a> </li>

                       </ul>
                       <!-- End Nav Right -->
                    </div>
                    <div class="col-md-7 col-sm-7 col-xs-12 col-collapse">
                       <!-- Collapse -->
                       <div id="navbar-rede" class="navbar-collapse collapse" aria-expanded="false">
                          <!-- Menu Dropdown -->
                          <ul class="nav navbar-nav">
                             <li class="visible-xs">
                                <div class="col-xs-12 input-group mobile-search-bar-container">
                                   <div class="mobile-search-bar">
                                      <input type="text" class="col-xs-10" placeholder="Digite aqui o que procura..." />
                                      <a href="#" class="col-xs-1"></a>
                                   </div>
                                </div>
                             </li>
                             <!-- Produtos -->
                             <li id="dropdown-produtos" class="dropdown dynamic-image-dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Nossos Produtos</a>
                                <!-- Dropdown container -->
                                <ul class="dropdown-menu dropdown-menu-produtos">
                                   <!-- Links -->
                                   <div class="col-links">
                                      <li data-dropdown-link="0" class="dropdown-item active"> <a href="produto-celular.html">Para celular</a></li>
                                      <li data-dropdown-link="1" class="dropdown-item"> <a href="#" >Sem fio</a> </li>
                                      <li data-dropdown-link="2" class="dropdown-item"> <a href="#" >Com fio</a> </li>
                                      <li data-dropdown-link="3" class="dropdown-item">  <a href="#">Vendas online</a> </li>
                                      <li data-dropdown-link="4" class="dropdown-item"> <a href="#" >Grandes empresas</a> </li>
                                      <li data-dropdown-link="5" class="dropdown-item">  <a href="#" >Adicionais</a> </li>
                                      <li data-dropdown-link="6" class="dropdown-item"> <a href="#">Todos os produtos</a> </li>
                                   </div>
                                   <div class="col-links">
                                      <li class="dropdown-subitem"> <a href="#">Nome Produto</a></li>
                                      <li class="dropdown-subitem"> <a href="#" >Nome Produto</a> </li>
                                      <li class="dropdown-subitem"> <a href="#" >Nome Produto</a> </li>
                                      <li class="dropdown-subitem">  <a href="#">Nome Produto</a> </li>
                                      <li class="dropdown-subitem"> <a href="#" >Nome Produto</a> </li>
                                      <li class="dropdown-subitem">  <a href="#" >Nome Produto</a> </li>
                                      <li class="dropdown-subitem"> <a href="#">Nome Produto</a> </li>
                                   </div>

                                   <!-- End Links -->
                                   <!-- Imagem -->
                                   <div class="col-img hidden-sm hidden-xs"> 
                                      <img data-related-link="0" class="img-active" src="images/menu_maquininha_celular.png" > 
                                      <img data-related-link="1" class="" src="images/menu_maquininha_celular_2.png" >
                                      <img data-related-link="2" class="" src="images/menu_maquininha_celular_3.png" >
                                      <img data-related-link="3" class="" src="images/menu_maquininha_celular_4.png" >
                                      <img data-related-link="4" class="" src="images/menu_maquininha_celular.png" >
                                      <img data-related-link="5" class="" src="images/menu_maquininha_celular_2.png" >
                                      <img data-related-link="6" class="" src="images/menu_maquininha_celular_3.png" >
                                   </div>
                                   <!-- End Imagem -->
                                </ul>
                                <!-- End Dropdown container -->
                             </li>
                             <!-- End Produtos -->
                             <!-- Tipos Negócio -->
                             <li id="dropdown-tipos-negocio" class="dropdown dynamic-image-dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Tipos de Negócio</a>
                                <!-- Dropdown container -->
                                <ul class="dropdown-menu">
                                   <!-- Links -->
                                   <div class="col-links">
                                      <li data-dropdown-link="0" class="dropdown-item active"> <a href="#">Restaurantes, Bares</a> </li>
                                      <li data-dropdown-link="1" class="dropdown-item"> <a href="#">Táxis</a> </li>
                                      <li data-dropdown-link="2" class="dropdown-item"> <a href="#">Lojistas</a> </li>
                                      <li data-dropdown-link="3" class="dropdown-item"> <a href="#">Vendedores ambulantes, feirantes</a> </li>
                                      <li data-dropdown-link="4" class="dropdown-item"> <a href="#">Serviços de bem-estar e beleza</a> </li>
                                      <li data-dropdown-link="5" class="dropdown-item"> <a href="#">Lojas online</a> </li>
                                      <li data-dropdown-link="6" class="dropdown-item products-2"> <a href="#">Não encontrou seu tipo de negócio? <br/>
                                         Veja <span class="bold">Nossos produtos</span></a>
                                      </li>
                                   </div>
                                   <!-- End Links -->
                                   <!-- Imagem -->
                                   <div class="col-img hidden-sm hidden-xs"> 
                                      <img data-related-link="0" class="img-active" src="images/menu_tipos_negocio.jpg"  > 
                                      <img data-related-link="1" src="images/menu_tipos_negocio_2.jpg" >
                                      <img data-related-link="2" src="images/menu_tipos_negocio_3.jpg" >
                                      <img data-related-link="3" src="images/menu_tipos_negocio_4.jpg" >
                                      <img data-related-link="4" src="images/menu_tipos_negocio_5.jpg" >
                                      <img data-related-link="5" src="images/menu_tipos_negocio_6.jpg" >
                                      <img data-related-link="6" src="images/menu_tipos_negocio_7.jpg" >
                                   </div>
                                   <!-- End Imagem -->
                                </ul>
                                <!-- End Dropdown container -->
                             </li>
                             <!-- End Tipos Negócio -->
                          </ul>
                          <!-- End Nav Right -->
                       </div>
                       <!-- End Collapse -->
                    </div>
                 </div>
              </nav>
           </div>
        </header>   

1 个答案:

答案 0 :(得分:0)

解决了它。

非常简单。只需将函数放在AJAX调用的done函数中。

感谢所有帮助过我的人!