javascript - 选择 Div 行以一次突出显示它

标签 javascript jquery html

[在此处输入图像描述][1]我正在创建一个检查预约,每次单击一个时,我都希望将所选行突出显示为绿色。行中的数据来自数据库。我尝试将类直接放在 div 内,但两行突出显示。

提前谢谢

这是输出

 $(document).on('click', '.selectdate', function(){


  var selected = $('#walakapake').hasClass("highlight");


$("walakapake").removeClass("row");
        if(!selected)
        {
            $('#walakapake').addClass("row highlight");
        }

  
 });
<style type="text/css">
  

#table-wrapper {
  position:relative;
}
#table-scroll {
  height:500px;
  overflow:auto;  
  margin-top:20px;
}
#table-wrapper table {
  width:100%;

}

#walakapake:hover{
background-color: #fafafa;
}

.highlight { background-color: red; }

</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js">

<link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'>



</script>
<div id="table-wrapper">
  <div id="table-scroll">

  <div class="container" >


 
<a name="submitting" class="selectdate" id="1">
<div class="row" style="border-style: solid; border-width: 1px; cursor: pointer; " id="walakapake" >
      <div class="col-2 text-right">
      <br>
        <h1 class="display-4"><span class="badge badge-info">24</span></h1>
        <h2  style="text-transform: uppercase;" id="month_name" >NOV</h2>
      </div>
      <div class="col-10">
      <br>
        <h3 class="text-uppercase"><strong>Examination day</strong></h3>
        <ul class="list-inline">
            <li class="list-inline-item"><i class="fa fa-calendar-o" aria-hidden="true"></i> Sunday  | 2019</li>
          <li class="list-inline-item"><i class="fa fa-clock-o" aria-hidden="true"></i> 8:00AM - 3:00PM</li>
          <li class="list-inline-item" style="color: orange;" ><i class="fa fa-location-arrow" aria-hidden="true" style="color: black;" ></i> Slot  32</li>
        </ul>
        <p>Please bring your requirements</p>
        <br>
      </div>
    </div>
    </a>
    
  <br> 
<br>

<a name="submitting" class="selectdate" id="2">
<div class="row" style="border-style: solid; border-width: 1px; cursor: pointer; " id="walakapake" >
      <div class="col-2 text-right">
      <br>
        <h1 class="display-4"><span class="badge badge-info">23</span></h1>
        <h2  style="text-transform: uppercase;" id="month_name" >NOV</h2>
      </div>
      <div class="col-10">
      <br>
        <h3 class="text-uppercase"><strong>Examination day</strong></h3>
        <ul class="list-inline">
            <li class="list-inline-item"><i class="fa fa-calendar-o" aria-hidden="true"></i> Saturday  | 2019</li>
          <li class="list-inline-item"><i class="fa fa-clock-o" aria-hidden="true"></i> 8:00AM - 3:00PM</li>
          <li class="list-inline-item" style="color: orange;" ><i class="fa fa-location-arrow" aria-hidden="true" style="color: black;" ></i> Slot  32</li>
        </ul>
        <p>Please bring your requirements</p>
        <br>
      </div>
    </div>
    </a>
    
    
    
    
    
    
    
  </div>
</div>
</div>

最佳答案

$('.foo').click(function() {
$('.foo').removeClass('highlight'); //Removes every highlight
$(this).addClass('highlight'); //sets highlight to the clicked one
});
.highlight {
  background-color:yellow;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li class="foo">Element 1</li>
  <li class="foo">Element 2</li>
  <li class="foo">Element 3</li>
</ul>

关于javascript - 选择 Div 行以一次突出显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58985926/

相关文章:

javascript - 删除所有按下的按键事件

javascript - 输入框的值在 4 个数字后重置

javascript - Nextjs - 在服务器端访问 url 参数

javascript - 是否有任何跨浏览器的 javascript 使 vh 和 vw 单位工作

javascript - 如何使用增量数字进行数学运算并在 jQuery 中更新结果

html - 如何自定义 bootstrap 默认顶部固定导航,如下图所示?

javascript - 如果父标签包含 ng-bind-html 指令,则没有 html 标签起作用

javascript - 如何在 Collapse Jquery 中维护状态

javascript - 在 javascript 中获取 tinyEditor 值

javascript - HEAD 中 LINK 和 STYLE 元素的评估顺序