简体   繁体   English

如何避免引导下拉列表中的子元素的点击事件

[英]How to avoid click event for child elemnets in bootstrap dropdown

I am using bootstrap dropdown in my project. 我在项目中使用bootstrap下拉列表。

<ul class="dropdown-menu">
            <li class="favProperties dropdown-submenu pull-left"><a href="#">Property</a>
<ul>
<li> property 1 </li>
<li> property 2 </li>
<li> property 3 </li>
</ul>
</li>
        </ul>

I have onclick event on "li" tag having class "favProperties". 我在“ li”标签上具有类“ favProperties”的onclick事件。

$(".favProperties").on("click", function (e) {
alert("clicked");
});

This work fine when I click on parent Li tag. 当我单击父Li标签时,此工作正常。 But this click event is also fired when I click on child Li tags. 但是,当我单击子Li标签时,也会触发此click事件。

How can I avoid these click events. 如何避免这些点击事件。

$(".dropdown-menu li.favProperties").on("click", function (e) {
    e.preventdefault()
});

The event.preventDefault() method stops the default action of an element from happening.The event parameter comes from the event binding function. event.preventDefault()方法停止发生元素的默认操作。event参数来自事件绑定函数。 The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. preventDefault()方法将取消事件(如果可取消),这意味着不会发生属于该事件的默认操作。

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

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