从jQuery中的event.target获取类名后如何使用类名?

时间:2019-01-24 15:39:02

标签: jquery html

我在HTML上有以下代码:

$(".readMorebtn_id").on('click', function(e) {
  var icon = e.target;

  var parentDiv = $(icon).parent();
  var child = $(parentDiv).children(".afterReadMore_Class");
  var cls = $(child).attr('class');
  console.log(cls);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="readMorediv_id">
  <button class="readMorebtn_id">Read More</button>
  <div class="afterReadMore_Class" style="display: none !important;">
    <div>
      <span><b>User CNIC:</b></span><span><?php  $row['usercnic']?></span>
    </div>
    <div>
      <span><b>Father CNIC:</b></span><span><?php echo$row['fathercnic']?> 
         </span>
    </div>
  </div>
</div>

在jQuery中,我正在执行“ console.log(cls)”的类名称显示在控制台框中。但我想在进一步的过程中使用此类名称。

1 个答案:

答案 0 :(得分:2)

我认为您需要类似的东西:

$(".readMorebtn_id").on('click', function(){
    $(this).parent().find(".afterReadMore_Class").show();
});

这将显示单击的“ readMorebtn_id”的相关“ afterReadMore_Class”。

$(".readMorebtn_id").on('click', function() {
  $(this).parent().find(".afterReadMore_Class").show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="readMorediv_id">
  <button class="readMorebtn_id">Read More</button>
  <div class="afterReadMore_Class" style="display: none !important;">
    <div>
      <span><b>User CNIC:</b></span><span><?php  $row['usercnic']?></span>
    </div>
    <div>
      <span><b>Father CNIC:</b></span><span><?php echo$row['fathercnic']?> 
 </span>
    </div>
  </div>
</div>
<div id="readMorediv_id">
  <button class="readMorebtn_id">Read More</button>
  <div class="afterReadMore_Class" style="display: none !important;">
    <div>
      <span><b>User CNIC:</b></span><span><?php  $row['usercnic']?></span>
    </div>
    <div>
      <span><b>Father CNIC:</b></span><span><?php echo$row['fathercnic']?> 
 </span>
    </div>
  </div>
</div>
<div id="readMorediv_id">
  <button class="readMorebtn_id">Read More</button>
  <div class="afterReadMore_Class" style="display: none !important;">
    <div>
      <span><b>User CNIC:</b></span><span><?php  $row['usercnic']?></span>
    </div>
    <div>
      <span><b>Father CNIC:</b></span><span><?php echo$row['fathercnic']?> 
 </span>
    </div>
  </div>
</div>