-2
You can do with index():
$('div').on('click', function() {
alert('O elemento encontra-se na posição ' +$(this).index());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
If you want a given action to occur on a given element based on its index (position within a vector) you can do:
$('div').eq(2).on('click', function() {
alert('Clicou no elemento ' +$(this).index());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
In this case the click event was only delegated to the third div.
You can also use Child css selectors
$('div:nth-child(3)').on('click', function() {
alert('Clicou no elemento ' +$(this).index());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
<div>Hey</div>
https://api.jquery.com/index/
– Jean Gustavo Prates
Never use code in image copy paste
– Leone Cerqueira
It is impossible for me to do a mouse effect on the image when it gets dark by cutting the image. as it does?
– Jessica Jadex