在Jquery.addClass()和.removeClass()函数中,如果先前的

50few1ms  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(155)

我有一个问题,通过单击列表组id_amici我从数据库发送id并插入addClass(),通过再次单击我发送新id的问题,我不能使用.removeClass()函数删除以前的id,我该怎么做?谢谢你

----- HTML PHP --------

$query_logo ="SELECT `ph_banner` FROM `fli_foto_utente` WHERE `id-utente`= '" . $id_utente . "'";
  $rs_logo = mysql_query ($query_logo, $conn);
  $check_foto = false;
  while( $rows_logo = mysql_fetch_assoc($rs_logo) ) {
    if ( $rows_logo['ph_banner'] == 'logo' ) {
      $check_foto = true;
    }
  }
  $img_user = "assets/img/user.png";

  if ( $check_foto ) {
    $img_user = "assets/upload/foto_utente/id_ph_" . $id_utente . "/" . $rows_logo['foto-utente'];      
  } else {
    switch ( $rows_utente['genere'] ) {
      case 'maschile':
        $img_user = "assets/img/man.png";
        break;
      case 'femminile':
        $img_user = "assets/img/girl.png";
        break;
      case 'altro':
      default:
        $img_user = "assets/img/user.png";
        break; 
    }
  }

  ?>

        <a href="#" class="list-group-item list-group-item-action" id="ev-chat_<?php echo $rows_amici['amici_id']; ?>" data-id_amici="<?php echo $rows_amici['amici_id']; ?>">          
           <span class="chat-img"> <img src="<?php echo $img_user; ?>" alt="" class="img-fluid" ></span> <?php echo $nomeandcogn; ?>
           
        </a>
        
  <?php 
}

----- Jquery ------

$('[id*="ev-chat_"]').click(function() {

      var clickCount = 0;

      var id_amici = $(this).data("id_amici");      
      
      $('#ev-chat_' + idamici).removeClass("active-plus");      

      clickCount++;

      if (clickCount != String(1)) {
        $('#ev-chat_' + id_amici).prev().removeClass('active-plus');

      }else if (clickCount == String(1)){

        $('#ev-chat_' + id_amici).addClass('active-plus');
      }

 });

字符串

nqwrtyyt

nqwrtyyt1#

$("#container .list-group-item").click(function() {
    var $this = $(this);
    var clickCount = $this.data('click_count') || 0;

    $this.removeClass("active-plus");      

    clickCount++;

    if (clickCount == 1){
      $this.addClass('active-plus');
    } else {
      $this.prev().removeClass('active-plus');
    }
    
     $this.data('click_count', clickCount);
 });
 
 $("#radio .list-group-item").click(function() {
    $("#radio .list-group-item").removeClass("active-plus");      
    $(this).addClass("active-plus");
 });
.active-plus {
  border: 1px solid red;
}

a {
  margin:0 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <a href="#" class="list-group-item list-group-item-action" id="ev-chat_123" data-id_amici="123">         
    <span class="chat-img"> 
      <img src="" alt="" class="img-fluid" >
    </span> 
    $nomeandcogn
  </a>
  <a href="#" class="list-group-item list-group-item-action" id="ev-chat_456" data-id_amici="456">         
    <span class="chat-img"> 
      <img src="" alt="" class="img-fluid" >
    </span> 
    $nomeandcogn
  </a>
</div>
<hr/>
<div id="radio">
  <a href="#" class="list-group-item list-group-item-action" id="ev-chat_123" data-id_amici="123">         
    <span class="chat-img"> 
      <img src="" alt="" class="img-fluid" >
    </span> 
    Radio1
  </a>
  <a href="#" class="list-group-item list-group-item-action" id="ev-chat_456" data-id_amici="456">         
    <span class="chat-img"> 
      <img src="" alt="" class="img-fluid" >
    </span> 
    Radio2
  </a>
  <a href="#" class="list-group-item list-group-item-action" id="ev-chat_456" data-id_amici="456">         
    <span class="chat-img"> 
      <img src="" alt="" class="img-fluid" >
    </span> 
    Radio3
  </a>
</div>

clickCount始终为1
$('#ev-chat_' + idamici).removeClass("active-plus");变量idamici,而不使用_

相关问题