2014-02-21 65 views
-1

我需要從div中返回文本。 當我點擊點擊我,我想從div class='show_mail_middle'從div中返回文本時點擊這個div的一些鏈接

<div class='show_mail'> 
<div class='show_mail_top'><p class='cli'>click me</p></div> 
<div class='show_mail_middle'>1</div> 
</div> 


<div class='show_mail'> 
<div class='show_mail_top'><p class='cli'>click me</p></div> 
<div class='show_mail_middle'>2</div> 
</div> 

<div class='show_mail'> 
<div class='show_mail_top'><p class='cli'>click me</p></div> 
<div class='show_mail_middle'>3</div> 
</div> 

與文字來提醒感謝您的幫助!

+2

你有沒有發現任何有用以下問題的答案? – lshettyl

回答

0
$('body').on("click",".show_mail_top",function(){ 
    alert($(this).find("p").text()); 
}); 
0

Fiddle Demo

$('.cli').click(function() { 
    alert($(this).closest('div').next().text()); 
}); 


Fiddle Demo

$('.cli').click(function() { 
    alert($(this).parent().next().text()); 
      //^get parent element div of current element with class .cli which 
//clicked and than find next div element n get it's text 
}); 


.closest()

.next()

this keyword

0

您可以使用.closest() & .next()

組合使用

$('.cli').click(function() { 
    alert($(this).parent().next('.show_mail_middle').text()); 
}); 

DEMO

OR

$('.cli').click(function() { 
    alert($(this).closest('.show_mail_top').next('.show_mail_middle').text()); 
}); 

DEMO 2

0
$(".show_mail_top").click(function() { 
    $(this).parent().children(".show_mail_middle").text(); 
}); 
0

嘗試:

$('p.cli').click(function(){ 
    alert($('div.show_mail_middle').text()) 
}); 
0

Demo

$('.cli').click(function() { 

    var text = $(this).parents('.show_mail').find('.show_mail_middle').html(); 

    alert(text); 
}); 
1

使用由純JavaScript。

var allDiv = document.getElementsByClassName('show_mail_top'); 
for(var i=0; i<allDiv.length; i++){ 
    allDiv[i].onclick = function(){ 
     alert(this.nextElementSibling.innerHTML); 
    } 
} 

DEMO