在与jQuery的链接后选择类“ x”的第一个元素
-
19-09-2019 - |
题
我有这样的代码:
<p>Lorem ipsum<a href=# class="a">link</a><span class="b">Text</span>More lorem ipsum<a href=# class="a">link</a><span class="b">Text 2</span>
我想让用户点击 link
并能够用跨度做一些事情(使其闪烁,将其隐藏,无论如何)。我正在使用jQuery,这就是我到目前为止所拥有的:
$('.a').click(function() {
$(this).nextUntil('.a').css('background-color', 'red');
});
只要我努力在跨越跨度之前链接,而文本中没有其他代码,则可以使用。但是,我对此不满意,必须有更好的方法。我也尝试了:
$('.a').click(function() {
$(this).next('.b').css('background-color', 'red');
});
但这选择了班级的所有跨度 b
, ,所以没有好处。
我是新手,茫然。我寻求智慧。
解决方案
$('a.a').click(function() {
$(this).next('span.b').css('background-color', 'red');
});
应该对你有好处。它立即得到兄弟姐妹。
其他提示
也许您可以尝试一下?
$(".a").click(function() {
$(this).addClass("something_special");
$(".something_special + .b").css({backgroundColor: "red"});
$(this).removeClass("something_special");
});
不隶属于 StackOverflow