gpt4 book ai didi

preventDefault does not work after add class to element(将类添加到元素后,预防默认设置不起作用)

转载 作者:bug小助手 更新时间:2023-10-25 11:53:44 25 4
gpt4 key购买 nike



I need to add class on span click which will change the icon but not make the span clickable which is work without adding class.

我需要在跨度点击添加类,这将改变图标,但不会使跨度可点击,这是工作没有添加类。


So..:

所以……:


<script>
$('body').on('click', '.royalonogy-mobile-menu-link', function(e){
if(e.target.className=='royalonogy-mobile-menu-dropdown')
e.preventDefault();
});
jQuery(document).ready(function () {

jQuery("#royalonogy-mobile-menu .royalonogy-mobile-menu-item .royalonogy-mobile-menu-link .royalonogy-mobile-menu-dropdown").on('click', function () {
jQuery(this).parent().next('ul.royalonogy-mobile-podkategorie-menu').toggle();
</script>

This code above works well, it show or hide me dropdown menu items. But now I need to add class royalonogy-mobile-menu-dropdown-active to that span element and keep the link not clickable, so I tried this after that event which i want to do after toggle:

上面的代码运行良好,它可以显示或隐藏下拉菜单项。但现在我需要向该span元素添加class royalonology-Mobile-Menu-DropDown-Active,并保持该链接不可点击,因此我在该事件之后尝试了此操作,我希望在切换后执行以下操作:


if (jQuery(this).hasClass("royalonogy-mobile-menu-dropdown-active")) {
jQuery(this).removeClass("royalonogy-mobile-menu-dropdown-active");
} else {
jQuery(this).addClass("royalonogy-mobile-menu-dropdown-active");
}

but after this I'm clicking on the span and it redirects me to that link which I do not want - royalonogy-mobile-menu-link is <a> element which contains link and span element (class: .royalonogy-mobile-menu-dropdown) with icon.

但在此之后,我点击SPAN,它会将我重定向到我不想要的链接-royalonology-mobile-menu-link是元素,它包含带有图标的link和span元素(类:.royalonology-Mobile-Menu-Dropdown)。


更多回答
优秀答案推荐

To disable a default action return false:

要禁用默认操作,请返回FALSE:


$('body').on('click', '.royalonogy-mobile-menu-dropdown', e => 
$(e.target)
.toggleClass('royalonogy-mobile-menu-dropdown-active')
.parent().next('.royalonogy-mobile-podkategorie-menu').toggle()
&& false
);

更多回答

it working well, just needed to remove the ; after false

它工作得很好,只需要去掉;错误后

@MartinOrešanský, oops, yes, a leftover

@MartinOrešanskç,哎呀,是的,一个剩饭

@MartinOrešanský you can now also upvote the answers (the arrow up) 😉

@😉Orešanskä你现在也可以在答案上加注(向上的箭头)martin

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com