我环顾四周,但没有发现任何可以真正解决我遇到的问题的东西,虽然我可以读写基本的 JS,但我仍然发现调试我自己的问题很棘手。
我试图从元素 onclick 中删除一个类,但由于某些原因 .removeClass
没有按预期工作。我已经在使用一些 JS 来触发样式更改(还有 onclick),所以我知道元素 ID 和类是正确的。
触发元素:#gsbtns1 .vc_btn3-color-grey
要删除的类:.nest-custom-activator
这是我工作“风格”JS 的一部分:
<script>
jQuery('#gsbtns1 .vc_btn3-color-grey').click(function() {
jQuery('#gsbtns1 .vc_btn3-color-grey').css({
'opacity': '0.3',
'cursor': 'auto',
'pointer-events': 'none'
});
});
</script>
这是控制台中触发元素的屏幕截图和文本副本:
控制台截图:
控制台文本:
<div class="vc_btn3-container nest-custom-activator
wpb_animate_when_almost_visible wpb_fadeIn fadeIn vc_btn3-inline
vc_custom_1505915503693 wpb_start_animation animated" id="gsbtns1">
我试过这个 JS:
<script>
jQuery('#gsbtns1 .vc_btn3-color-grey').click(function() {
jQuery(".vc_btn3-color-grey").removeClass("nest-custom-activator");
});
</script>
和这个 JS:
<script>
jQuery('#gsbtns1 .vc_btn3-color-grey').click(function() {
jQuery("#gsbtns1 .vc_btn3-color-grey").removeClass("nest-custom-activator");
});
</script>
遗憾的是,这些都没有删除 .nest-custom-activator
类。
谁能帮我理解为什么这不起作用?
最佳答案
您的屏幕截图显示它是父元素而不是当前元素。因此,您需要:
jQuery('#gsbtns1 .vc_btn3-color-grey').click(function() {
jQuery(this).parent().removeClass("nest-custom-activator");
});
关于javascript - 使用 JS 删除元素类 onclick(不工作)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46449294/