gpt4 book ai didi

javascript - HTML制作菜单展开/折叠

转载 作者:行者123 更新时间:2023-11-30 21:21:34 30 4
gpt4 key购买 nike

我试图在单击“项目”按钮时使“javascript 项目”和“CGI 项目”滑动切换。但是,我不太明白为什么点击时只有CGI项目按钮切换,而javascript项目按钮保持不变?

我正在尝试使用 Jquery 来实现滑动切换效果,但似乎只有 CGI 项目有反应,但两个按钮具有完全相同的 HTML 代码和 css。有什么区别??

<!Doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
*{box-sizing: border-box}
body {font-family: "Lato", sans-serif;}

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

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

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

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

.projectList {
display: none;
}

/* Style the tab content */
.tabContent {
float: left;
padding: 0px 12px;
width: 70%;
height: 500px;
}

li {
list-style-type: none;
}
</style>
</head>

<body>
<nav>
<div class="tab">
<button class="tabLinks" onclick="openInfo(event, 'About Myself')" id="defaultOpen">About Myself</button>
<button class="tabLinks" id="projects">Projects
<li id="java"><button class="tabLinks" onclick="openInfo(event, 'javascript project')">Javascript project</button></li>
<li id="cgi"><button class="tabLinks" onclick="openInfo(event, 'cgi project')">CGI project</button></li>
</button>
<button class="tabLinks" onclick="openInfo(event, 'Contact')">Contact</button>
</div>

<div id="About Myself", class="tabContent">
<h3>About Myself</h3>
<p>This is my information section</p>
</div>

<div id="Contact", class="tabContent">
<h3>Contact</h3>
<p>This it contact section</p>
</div>

<div id="javascript project", class="tabContent">
<h3>Javascript Project</h3>
<p>This is javascript project section</p>
</div>

<div id="cgi project", class="tabContent">
<h3>CGI project</h3>
<p>This is the CGI project section</p>
</div>
</nav>

<script>
function openInfo(evt, diplayInfo) {
var i, tabcontent, tablinks;

// Get all elements with class="tabContent" and hide them
tabcontent = document.getElementsByClassName("tabContent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}

// Get all elements with class="tabLinks" and remove the class "active"
tablinks = document.getElementsByClassName("tabLinks");
for(i=0; i<tablinks.length; i++){
tablinks[i].className = tablinks[i].className.replace(" active", "");
}

// Show the current tab, and add an "active" class to the link that opened the tab
document.getElementById(diplayInfo).style.display = "block";
evt.currentTarget.className += " active";
}

$(document).ready(function(){
$("#projects").on("click", function(){
$("#java").slideToggle(500);
$("#cgi").slideToggle(500);
});
});

document.getElementById("defaultOpen").click();
</script>

</body>
</html>

最佳答案

<button class="tabLinks" id="projects">Projects</button>
<ul>
<li id="java">
<button class="tabLinks" onclick="openInfo(event, 'javascript project')">Javascript project</button>
</li>
<li id="cgi">
<button class="tabLinks" onclick="openInfo(event, 'cgi project')">CGI project</button>
</li>
<ul>

关于javascript - HTML制作菜单展开/折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45133722/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com