【问题标题】:jQuery on(click)/click event not firingjQuery on(click)/click 事件未触发
【发布时间】:2017-03-01 07:58:28
【问题描述】:

我在 UI 设计方面非常糟糕,所以我使用 bootstrap 和 jquery 来帮助我一点。 我正在尝试将 jQuery 事件绑定到导航栏,因此当用户单击导航栏上的 p 标签时,它应该触发单击事件。但是,事实并非如此。无论我使用 jQuery("#logOff").click() 还是 on("click","#logOff",function()) 它只是没有触发事件。 下面是我的示例代码。

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");
  });
});

这是我的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>

【问题讨论】:

    标签: javascript jquery html bootstrap-modal


    【解决方案1】:

    尝试从您的 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>
    

    # 在选择器中用于搜索 id,所以 jQuery 选择器中的 #logoff 表示 使用 id="logoff" 查找元素id="#logoff" 不匹配 p>

    【讨论】:

    • 天啊,我多么愚蠢,我花了 6 个小时搜索但没有检查我的代码。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2014-07-02
    • 1970-01-01
    相关资源
    最近更新 更多