单击另一个同级链接时获取同级链接类?
我有 5 个 div,每个 div 有两个链接,如下例所示,
<div id="wrapper">
<a href="2332" class="first link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
<div id="wrapper">
<a href="1221" class="second link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
<div id="wrapper">
<a href="789" class="third link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
<div id="wrapper">
<a href="345" class="abc link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
<div id="wrapper">
<a href="123" class="bcd link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
当单击 LINK2 时,如何使用 javascript/jquery 获取 link1 的类(在同一 div 内而不是其他 div 内)。 好吧,当我点击 third div 的 link2 链接时,它会给我“third”(不包括 link1,它也是一个类)。
点击函数为:
$('.link2').click(function(){
});
i have 5 divs , in each divs there's two links as shown in the example below
<div id="wrapper">
<a href="2332" class="first link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
<div id="wrapper">
<a href="1221" class="second link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
<div id="wrapper">
<a href="789" class="third link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
<div id="wrapper">
<a href="345" class="abc link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
<div id="wrapper">
<a href="123" class="bcd link1">Link1</a><a href="link2" class="link2">Link2</a>
</div>
How can i use javascript/jquery to GET the CLASS of link1(inside the same div not others) when LINK2 is clicked.
Okay when i clicked the third div's link2 link , it will give me "third" (excluding the link1 which is also a class).
the click function is:
$('.link2').click(function(){
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为第一个链接紧邻第二个链接,所以您可以使用
prev
来获取到链接和attr
获取class 的值
属性:这将为您提供“第三个链接1”,因为这是
class
属性的值。如果您只想要第一部分,可以将其拆分:这是一个工作示例。
Because the first link immediately precedes the second, you can use
prev
to get to the link andattr
to get the value of theclass
attribute:That will give you "third link1", because that's the value of the
class
attribute. If you only want the first part, you could split it:Here's a working example.
看一下 jQuery siblings 选择器。您还可以根据自己的情况使用上一个或下一个。
Take a look at jQuery siblings selector. You can also use prev or next depending on your situation.