gpt4 book ai didi

javascript - jQuery - addClass 不向 html 元素添加类

转载 作者:行者123 更新时间:2023-11-28 01:14:15 25 4
gpt4 key购买 nike

我尝试将 CSS 类添加到 <li>元素,当我单击按钮但 addClass 不起作用时。

这是我的 JS:

$('.test').click(function(event) {
var centrum1 = $('.p17');
$('section.bok-map').find( centrum1 ).addClass('active-region');
});

这是 HTML 代码的样子: enter image description here

问题出在哪里? find() 返回 true。
这是演示:http://demo.vrs-factory.pl/mapDemo/

最佳答案

您有几个错误,因为您没有选择正确的元素,因此 selector 的长度为 0。

首先,该类名为 pl7 而不是 p17,其次,当使用 removeClass 时,您不需要放置 在类名之前。当您使用 removeClass 时,可以理解您想要定位一个类,因此不需要您通过添加点来指定它。

 <script>
var centrum1 = $('.pl7');

$('.test').click(function(event) {
$('section.bok-map').find( centrum1 ).removeClass('pl7');
});

</script>

另外,可能值得注意的是,因为您只引用$(.pl7),所以您不必将它分配给变量。你也可以像下面这样写。它是由你决定。

  $('.test').click(function(event) {            
$('section.bok-map').find('.pl7').removeClass('pl7');
});

关于javascript - jQuery - addClass 不向 html 元素添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36384230/

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