4
Hello, I wonder if I’m misinterpreting the command, or if I’m doing something wrong in the implementation.
Freely translating, closest
means "closer". But by clicking on an element and asking for the closest()
of him, he brings me the very element.
In the following snippet, my intention was that he alert
in the counter of the previous line, however, if you click on line 3, it returns undefined
$('tr').click(function () {
var linha = $(this)
var cont = linha.attr('contador')
var ant = (cont - 1);
alert(
linha.closest('tr[contador="' + ant + '"]').attr('contador')
)
});
tr{
line-height: 40px
}
.vermelho{ background-color: red; }
.verde{ background-color: green; }
.azul{ background-color: blue; }
.amarelo{ background-color: yellow; }
.roxo{ background-color: purple; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="500px">
<tr id="tr-1" contador="1" class="vermelho"><td>teste 1</td></tr>
<tr id="tr-2" contador="2" class="azul"><td>teste 2</td></tr>
<tr id="tr-3" contador="3" class="verde"><td>teste 3</td></tr>
<tr id="tr-4" contador="4" class="amarelo"><td>teste 4</td></tr>
<tr id="tr-5" contador="5" class="roxo"><td>teste 5</td></tr>
</table>
To documentation on the website of JQuery
says the following:
Travels up the DOM Tree until it finds a match for the supplied selector
So (correct me if I’m wrong), he should find the top line, right? Since he walks the GIFT above him. Or am I misinterpreting? How the .closest()
then?
the
tr
that Voce has are allsiblings
, so returnundefined
– andrepaulo