18910140161

JavaScript-使用垂直选项卡菜单刷新或重新加载选项卡内容-堆栈溢出

顺晟科技

2022-10-19 12:27:26

77

##这是我有的##

  1. 我的代码只是显示我与php文件一起包含的php文件,所以当标签页加载时,它已经加载了数据。

  2. 所以在将数据插入数据库后,它仍然有旧数据来获取当前数据,我需要重新加载整个页面来获取新数据。

  3. 所以每次过程完成后,我都必须重新加载整个页面。

##这是我需要的##

  1. 每次选择Tab
  2. 时,我都需要加载包含在php中的页面

2.如果我只重新加载选定的标签页,那么我将在提交数据后获得新数据

  1. 然后我将获取当前数据以插入或删除

##描述##

  1. 在我的代码中,我使用了javascript for tab。如果您知道用jquery等任何其他语言解决我的问题,它也会很有用

顺晟科技:

为什么不使用jQuery而不是php呢?它可以满足您的需要,而且非常简单。

例如:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
body {font-family: "Lato", sans-serif;}

/* Style the tab */
.tab {
  float: left;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
  width: 30%;
  height: 300px;
}

/* Style the buttons inside the tab */
.tab button {
  display: block;
  background-color: inherit;
  color: black;
  padding: 22px 16px;
  width: 100%;
  border: none;
  outline: none;
  text-align: left;
  cursor: pointer;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current "tab button" class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  float: left;
  padding: 0px 12px;
  border: 1px solid #ccc;
  width: 70%;
  border-left: none;
  height: 300px;
}
</style>
</head>
<body>

<h2>Vertical Tabs</h2>
<p>This is code i am diplaying included php file which contains process of inserting ,deleting and updating</p>
<p>Currently after data submission or update i have to reload whole page to get current data for three tabs </p>
<p>I need to know how to refresh the selected tab php file to get the current data for selected tab witout reloading page</p>
<p>for example: after i insert data in insert tab after i select update tab it should load and show current data instead of showing old data</p>
<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">insert</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')">update</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')">delete</button>
</div>

<div id="London" class="tabcontent">
  <h3>insert</h3>
  <p>here will be my first php includes</p>
    <?php


include "insert.php"; ?>
</div>

<div id="Paris" class="tabcontent">
  <h3>update</h3>
  <p>here will be my second php includes</p>
<?php


include "update.php"; ?>
</div>

<div id="Tokyo" class="tabcontent">
  <h3>delete</h3>
  <p>here will be my third php includes</p>
<?php


include "delete.php"; ?>
</div>

<script>
function openCity(evt, cityName) {
  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(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}

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

您可以用它做很多事情,比如

  1. 在某个事件上加载文件。
  2. 按时间间隔(每5秒)加载一个文件。
  3. 在完成任务或进行更改并需要显示时加载文件。
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航