gpt4 book ai didi

javascript - 使用 .text() 选择一个类,但在 html 中存在空格问题

转载 作者:行者123 更新时间:2023-12-02 20:13:53 24 4
gpt4 key购买 nike

$('li.' + $(this).text()).toggle(true);

此代码用于从下面的子导航中获取。

<div class="subNav">
<ul>
<li class="button">sun protective clothing</li>
</ul>
</div>

这些是可见的产品。

<div class="sun-protective-clothing"></div>

最佳答案

您必须使用JavaScript的replace()函数将所有空格替换为破折号。请注意,像这样使用 replace("", "-") 只会替换字符串中的第一个空格。您必须使用带有全局搜索的正则表达式来替换所有实例;如//g

$('li.' + $(this).text().replace(/ /g, "-")).toggle(true);

关于javascript - 使用 .text() 选择一个类,但在 html 中存在空格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6600388/

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