div id with same name before the original div
Good morning, I have a question how do I get an id of div. One more after the initial div.
EX:
<div class="1">
<div class="2">
<div class="3">
<div class="4">
<div class="up" id="2"> UP </ div> <<- get id here
</ div>
</ div>
</ div>
</ div>
<div class="1">
<div class="2">
<div class="3">
<div class="4">
<div class="up" i开发者_如何学编程d="1"> UP </ div> <<- Click
</ div>
</ div>
</ div>
</ div>
I'll be grateful for the help.
Assuming the outer div always has the same class:
$('.up').click(function(){
alert( $(this).closest('.1').prev().find('.up').attr('id') );
});
function getId(){
var allDiv = document.getElementsByTagNames("div");
for(i=0; i<allDiv.lebgth; i++){
for(j=i; j<allDiv.lebgth; j++){
if(allDiv[i].className == allDiv[j].className )
return allDiv[i].getAttribute("id");
}
}
return "";
}
精彩评论