Javascript jQuery on(单击)/单击事件未触发
我非常不擅长UI设计,所以我使用了引导和jquery来帮助我。 我试图将jQuery事件绑定到导航栏,所以当用户单击导航栏上的p标记时,它应该触发一个单击事件。然而,事实并非如此。无论我使用jQuery(“#注销”)。单击()或打开(“单击”,“注销”,函数()) 它只是不射击事件。 下面是我的示例代码Javascript jQuery on(单击)/单击事件未触发,javascript,jquery,html,bootstrap-modal,Javascript,Jquery,Html,Bootstrap Modal,我非常不擅长UI设计,所以我使用了引导和jquery来帮助我。 我试图将jQuery事件绑定到导航栏,所以当用户单击导航栏上的p标记时,它应该触发一个单击事件。然而,事实并非如此。无论我使用jQuery(“#注销”)。单击()或打开(“单击”,“注销”,函数()) 它只是不射击事件。 下面是我的示例代码 jQuery(document).ready(function () { jQuery(document).on( 'click','#logOff', function (eve) {
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>
测试
链接
尝试从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”
不匹配,请尝试从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”不匹配的元素,天哪,我有多愚蠢,我花了6个小时搜索但没有检查我的代码。非常感谢你。天哪,我有多蠢,我花了6个小时搜索,但没有检查我的代码。非常感谢你。