JSP 点击选项卡时如何刷新选项卡内容?

nlejzf6q  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(162)
<div class="tab">
    <button class="tablinks" onclick="openMenu(event, 'Pending')">Pending</button>
    <button class="tablinks" onclick="openMenu(event, 'Completed')">Completed</button>
    <button class="tablinks" onclick="openMenu(event, 'Rejected')">Rejected</button>
    <button class="tablinks" onclick="openMenu(event, 'All')" id="defaultOpen">All</button>
</div>

<div id="Pending" class="tabcontent">
<%
 perform db selection pending records
%>

<div id="All" class="tabcontent">
<%
 perform db selection all records
%>

<script>
function openMenu(evt, Status) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
        
    }
    document.getElementById(Status).style.display = "block";
    evt.currentTarget.className += " active";
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>

参考https://www.w3schools.com/howto/howto_js_tabs.asp示例。我开发了一个选项卡来显示不同的状态内容。我将执行记录状态更改,但我不知道如何在某些记录状态更改时刷新选项卡内容。

tcomlyy6

tcomlyy61#

使用jQuery可以更快、更简单地执行操作:

<html>
<body>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<div class="tab">
    <button class="tablinks" data-val="Pending">Pending</button>
    <button class="tablinks" data-val="Completed">Completed</button>
    <button class="tablinks" data-val="Rejected">Rejected</button>
    <button class="tablinks active" data-val="All">All</button>
</div>

<div id="Pending" class="tabcontent">
<%
 perform db selection pending records
%>

<div id="All" class="tabcontent">
<%
 perform db selection all records
%>

<script>
$(".tablinks").click(function(){
  var val = $(this).attr("data-val");
  if(val=="All") $('.tabcontent').show();
  else {
      $('.tabcontent').hide();
      $('#'+val).show();
      $(".tablinks").removeClass("active");
      $(this).addClass("active");
  }
});
</script>
</body>
</html>

相关问题