我正在尝试使用javascript和php构建动态标签导航,我想每周一次在数据库中插入一个新标签。当我刷新页面时,它显示2个选项卡,当我单击2号选项卡时,它显示信息,当我单击选项卡1时,选项卡2消失。
$sql = "SELECT * FROM journal WHERE id=".$_SESSION['id'];
$sql = $conectar->prepare($sql);
$sql->execute();
$resultado = $sql->fetchAll();
for ($i=0;$i<count($resultado);$i++) {
?>
<button class="tablink" onclick="openPage('<?php echo $resultado[$i]['semana'] ?>')"><?php echo $resultado[$i]['semana'] ?></button>
<div id="<?php echo $resultado[$i]['semana'] ?>" class="tabcontent">
<h3><?php echo $resultado[$i]['semana'] ?></h3>
<p>Home is where the heart is..</p>
</div>
<?php } ?>
<script>
function openPage(pageName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = "";
}
document.getElementById(pageName).style.display = "block";
elmnt.style.backgroundColor = color;
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
我希望所有选项卡都可用于查看每周信息,当前,当我单击第一个选项卡时,第二个选项卡将消失。