简体   繁体   English

将类添加到jQuery中的活动列表项

[英]Add class to active list item in jquery

I have list in foreach loop in Php as like below 我在PHP的foreach循环中有如下列表

<?php 
     $cnt=1;
     foreach ($this->session->userdata('questions') as $row) {
         $i=1;
         foreach($row as $q) {      ?>
           <li id="<?php echo "number-". $q->subjectid."_".$i;?>" onclick="showQuestion('<?php echo "#".$q->subjectid."_".$i;?>');" class="btn bg-primary numbers z-answered"  >
            <?php echo $cnt++;?>
            </li>
            <?php 
            $i++;
         } 
    } 
?>

It will show question numbers from 1 to 100 (assume) 它将显示从1到100的问题编号(假设)

I want When a user will click any question, It will add demo css class to current question How to achieve it. 我想要当用户单击任何问题时,它将在当前问题中添加演示CSS类。

您可以使用jQuery的addClass()方法。

You can use jQuery for the purpose if you want to: 您可以使用jQuery来实现以下目的:

$(document).ready(function(){
  $('li').click(function(){
    $(this).addClass('demo');
    //code to remove class from rest of the <li>'s
    $(this).siblings().removeClass('demo');
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM