Javascript 跳过e.preventDefault();有条件地
我有以下html:Javascript 跳过e.preventDefault();有条件地,javascript,jquery,Javascript,Jquery,我有以下html: <ul id="menu" class="clearfix"> <li class="login-link"> <a href="/Account/Login">LOGIN AREA</a> </li> <li class="cart">
<ul id="menu" class="clearfix">
<li class="login-link">
<a href="/Account/Login">LOGIN AREA</a>
</li>
<li class="cart">
<span id="spCartCount">
1
</span>
<a href="#">
<img src="/images/cart.png"></a>
</li>
<li class="login-link">
<a href="/Account/LogOff">Log Out</a>
</li>
</ul>
当用户单击注销按钮时,由于e.preventDefault()的原因,他们不会重定向到注销页面;我想跳过e.preventDefault();如果用户单击“注销”
我试过这个:
$("#menu li:not(.cart) a").on("click", function (e) {
if ($("#menu li:not(.cart) a:contains('LogOff')")) {
} else {
e.preventDefault();
}
$("#menu li.cart > a").next(".popover").hide();
$(this).next(".popover").fadeToggle();
});
但它不起作用
如何操作?您需要检查当前单击的元素:
$("#menu li:not(.cart) a").bind("click", function (e) {
if ($(this).attr("href").indexOf("LogOff") < 0)
e.preventDefault();
$("#menu li.cart > a").next(".popover").hide();
$(this).next(".popover").fadeToggle();
});
$(“#菜单li:not(.cart)a”).bind(“click”,函数(e){
if($(this.attr(“href”).indexOf(“注销”)<0)
e、 预防默认值();
$(“#menu li.cart>a”).next(“.popover”).hide();
$(this.next(“.popover”).fadeToggle();
});
在事件触发器上,需要首先确定单击元素的位置,然后在href/class等中检查所需的值
“This”关键字可用于获取当前元素
$("#menu li:not(.cart) a").bind("click", function (e) {
if ($(this).attr('href').indexOf('LogOff') != -1)
e.preventDefault();
$("#menu li.cart > a").next(".popover").hide();
$(this).next(".popover").fadeToggle();
});
或
可以使用“e.target”
$("#menu li:not(.cart) a").bind("click", function (e) {
if ($(e.target).attr('href').indexOf('LogOff') != -1)
e.preventDefault();
$("#menu li.cart > a").next(".popover").hide();
$(this).next(".popover").fadeToggle();
});
但是标签上不包含“注销”。它包含“注销”。我想使用href或text执行此操作。href contains LoggOff
:contains
不检查属性。
$("#menu li:not(.cart) a").bind("click", function (e) {
if ($(e.target).attr('href').indexOf('LogOff') != -1)
e.preventDefault();
$("#menu li.cart > a").next(".popover").hide();
$(this).next(".popover").fadeToggle();
});