简体   繁体   English

jQuery on(click)/ click事件未触发

[英]jQuery on(click)/click event not firing

I am very bad at UI design so i used bootstrap and jquery to help me alittle bit. 我在UI设计上表现很差,所以我使用了bootstrap和jquery来帮助我。 I am trying to bind jQuery event to nav bar, so when user click p tag on nav bar it should fire a click event. 我正在尝试将jQuery事件绑定到导航栏,因此,当用户单击导航栏上的p标签时,它将触发一个click事件。 however, it doesn't. 但是,事实并非如此。 No matter i use jQuery("#logOff").click() or on("click","#logOff",function()) it just not firing event. 无论我使用jQuery(“#logOff”)。click()还是on(“ click”,“#logOff”,function()),它都不会触发事件。 below is my sample code. 下面是我的示例代码。

jQuery(document).ready(function () {


  jQuery(document).on( 'click','#logOff', function (eve) {
    alert("haha");
  });

  // this one not work too
jQuery("#logOff").click(function (e){
   alert("haha");
  });
});

this is my html 这是我的HTML

<nav class="navbar navbar-default  navbar-fixed-top ">
        <div class="navbar-header">
          <p class="navbar-brand" >test</p>
        </div>

        <div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">


          <div class="nav navbar-nav navbar-right">
            <li ><p id="#nickName" style="cursor: pointer" class="navbar-brand">Link</p></li>
            <li ><p id="#logOff" style="cursor: pointer" class="navbar-brand">log Off</p></li>
          </div>
        </div><!-- /.navbar-collapse -->

    </nav>

Try removing the # symbol from your id declarations - it's not required. 尝试从ID声明中删除#符号-这不是必需的。

<li ><p id="nickName" style="cursor: pointer" class="navbar-brand">Link</p></li>
<li ><p id="logOff" style="cursor: pointer" class="navbar-brand">log Off</p></li>

The # when used in selectors searches for ids, so #logoff in your jQuery selector is saying find the element with id="logoff" which won't match with id="#logoff" #在选择器中使用时会搜索ID,因此jQuery选择器中的#logoff表示查找id="logoff"id="#logoff"不匹配的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM