jQuery - 如何从div获取文本> p

时间:2015-03-10 19:00:40

标签: jquery html

如何从div类中获取主题名称?

<div class="subject "><img src="image/eng.jpg"/><p>English</p></div>
<div class="subject "><img src="image/bio.jpg"/><p>Biology</p></div>
<div class="subject "><img src="image/math.jpg"/><p>Mathematic</p></div>
<div class="subject "><img src="image/sci.jpg"/><p>Science</p></div>

因此,预期结果:您选择英语科目/您选择科学科目

$(document).ready(function() {
    $(".subject ").click(function(){
         // the script will declare here.
         alert('You are select'+subject+'subject ');
});

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
$(".subject ").click(function() {
  // the script will declare here.
  alert('You are select ' + $(this).find('p').text() + ' subject ');
});
&#13;
div {
  border:1px solid #999;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="subject ">
  <img src="image/eng.jpg" />
  <p>English</p>
</div>
<div class="subject ">
  <img src="image/bio.jpg" />
  <p>Biology</p>
</div>
<div class="subject ">
  <img src="image/math.jpg" />
  <p>Mathematic</p>
</div>
<div class="subject ">
  <img src="image/sci.jpg" />
  <p>Science</p>
</div>
&#13;
&#13;
&#13;