jua*_*tto 2 javascript jquery dom
我有此表,如何获取具有.cep类的第一个前任并通过Jquery获取此元素?
这是我的桌子:
<table>
<tr class="cep"></tr>
<tr class="ptype"></tr>
<tr class="item"></tr>
<tr class="cep"></tr> (I want to get this element)
<tr class="ptype"></tr>
<tr class="item"></tr>
<tr class="item-here"></tr> (I'me here)
<tr class="item"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点:
$('tr.item-here').prevUntilFindThefirstMatchOf('tr.cep'); Get this element
Run Code Online (Sandbox Code Playgroud)
请帮忙
我建议prevAll与结合使用first。 prevAll将通过选择器过滤所有以前的兄弟姐妹,并first仅捕获遇到的第一个兄弟姐妹。
$('tr.item-here').prevAll('tr.cep').first();
Run Code Online (Sandbox Code Playgroud)
$('tr.item-here').prevAll('tr.cep').first();
Run Code Online (Sandbox Code Playgroud)
var meh = $('tr.item-here').prevAll('tr.cep').first();
console.log($(meh).text());Run Code Online (Sandbox Code Playgroud)