gpt4 book ai didi

jquery - 试图从 jQuery 中具有两个类的元素中获取 1 个类

转载 作者:行者123 更新时间:2023-12-01 03:26:47 26 4
gpt4 key购买 nike

我想要定位一个设置了两个类名的元素:

例如。

<li style="display:none;" class="productList product-1">
<p class="removeIcon"><img src="images/remove-icon.jpg" align="Remove Product" /></p>
<span class="companyName">Company 1 goes here and here is a test for two lines</span>
</li>

当我尝试在 jQuery 中获取 li 的类时:

var parent = $(this).parent().get(0).className();

它返回“productList Product-1”我希望能够返回或减少输出为:“product-1”。我知道这可能很简单,但我现在不知道有什么方法。

我也需要它按照类定义的顺序。另一个功能是使用“productList”部分...

如果有办法的话,我可以只删除第一部分吗?

最佳答案

this.parentNode.className.split(' ')[1];

或者使用 jQuery:

$(this).parent().attr('class').split(' ')[1];

但我更喜欢第一个。

关于jquery - 试图从 jQuery 中具有两个类的元素中获取 1 个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4432382/

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