gpt4 book ai didi

javascript - 为什么 JavaScript 将元素写入控制台但不通过函数更新 css 显示值?

转载 作者:行者123 更新时间:2023-12-02 22:47:24 24 4
gpt4 key购买 nike

我正在作为小组的一员完成一项大学作业。当使用 JavaScript 更改按钮单击时的 css 属性时,我们遇到了某个事件未触发的问题。我们并不是在寻找任何人来编写/重写我们的代码以达到行业标准,但我们希望找出(在您的帮助下)我们哪里出了问题。

我们的任务是撰写报告并以网页格式呈现。我们使用 HTML/CSS 和少量 JavaScript 编写页面,用于下拉菜单和单击按钮时显示页面部分等内容。

我们无法解决的具体问题(代码如下)是,当单击菜单按钮时,JS 应填充页面标题并在正文中显示 div

我可以让它按预期工作,尽管只引用了 3 个 div (代码似乎忽略了第 3 个之后的数组元素),或者让它与所有标题一起工作,但没有引用了 div(如下)。

CMVE如下(完整代码如下):

let sections = ["Cooper", "Jenna", "Lyly", "Mick", "Samuel", "Stanton", "Welcome"];
var visSection = null;

/* var i;
for (i=0; i<sections.length;i++) {
console.log(sections[i]);
}
*/
function changeActiveSection(sectionID) {
if (visSection === sectionID) {
visSection = null;
} else {
visSection = sectionID;
}
hideSections();
}

function hideSections() {
var i, targetSection, headerText;
for (i = 0; i < sections.length; i++) {
targetSection = sections[i];
section = document.getElementById(visSection);
if (visSection === targetSection) {
// console.log(visSection, targetSection, section);
section.style.display = "block";
headerText = visSection;
} else {
section.style.display = "none";
}
populateHeader(headerText);
}
}

function populateHeader(headerText) {
var ob;
ob = document.getElementById("header-content");
ob.innerText = headerText;
}
<!doctype HTML>
<html>

<body>
<header>
<h1 id="header-content" style="display: block;">Welcome to The A2-Group-12 Team</h1>
</header>
<a href="#" onclick="changeActiveSection('Stanton')">Stanton</a>
<div id="Stanton" class="body-panel" style="display: none;">
<h2> this is a placeholder heading </h2>
<p> this is some placeholder body text. </p>
<p> Stanton </p>
</div>
<script>
//Insert JS from JS snippet
</script>
</body>

</html>

我觉得我们可能在某个地方误解了某种数据类型,但我们仍在学习,并且已经盯着这个问题好几天了(问题可能就在我们眼前!)。

<小时/>

带有 CSS 的完整代码片段:

let sections = ["Cooper", "Jenna", "Lyly", "Mick", "Samuel", "Stanton", "Welcome"];
var visSection = null;

var i;
for (i = 0; i < sections.length; i++) {
console.log(sections[i]);
}

function changeActiveSection(sectionID) {
if (visSection === sectionID) {
visSection = null;
} else {
visSection = sectionID;
}
hideSections(visSection);
}

function hideSections(sectionID2) {
var i, targetSection, headerText;
for (i = 0; i < sections.length; i++) {
targetSection = sections[i];
section = document.getElementById(sectionID2);
if (visSection === targetSection) {
console.log(visSection, targetSection, section);
section.style.display = "block";
headerText = visSection;
} else {
section.style.display = "none";
}
populateHeader(headerText);
}
}

function populateHeader(headerText) {
var ob;
ob = document.getElementById("header-content");
ob.innerText = headerText;
}

var dropdown = document.getElementsByTagName("button");
var i, objCol;

for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function() {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
});
}
/*Sets the font family for entire page.
removes entire margin for body section to create borderless display.*/

body {
font-family: Arial, Veranda, Serif;
margin: 0;
}


/*Sets attributes for the nav menu root element*/

#nav-menu {
height: 100%;
width: 15%;
position: fixed;
top: 0;
left: 0;
background-color: #353940;
overflow-x: hidden;
text-decoration: none;
text-align: center;
z-index: 1;
}


/*specifies hyperlink button text attributes*/

#nav-menu a {
text-decoration: none;
color: white;
font-size: 12pt;
display: block;
border-top: 1px solid white;
overflow: visible;
height: 20px;
padding-top: 5px;
padding-bottom: 5px;
}


/*Specifies most dropdown button attributes.*/

button.drop-button {
text-decoration: none;
background-color: #353940;
border: 0;
width: 100%;
color: white;
height: 20px;
outline: none;
font-size: 12pt;
border-top: 1px solid white;
overflow: visible;
padding-top: 5px;
padding-bottom: 5px;
}


/*Active class to highlight activated dropdown buttons*/

button.active {
background: #230fa8;
}


/*container class for dropdown menu items - hidden by default (display is manupulated via JS)*/

.dropdown-content {
display: none;
background-color: #4a5059;
}


/*specifies layout for dropdown menu items*/

.dropdown-content a {
display: block;
padding-top: 5px;
}


/*keeps header in line with rest of body*/

#header-content {
padding-left: 17%;
}


/*specifies positioning for body container divs*/

.body-panel {
position: absolute;
padding-left: 17%;
width: 75%;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" meta charset="utf-8" />
<title>Assignment 2 Group 12</title>
<link rel="stylesheet" href="css/style.css">
</head>

<body>
<header>
<h1 id="header-content" style="display: block;">Welcome to The A2-Group-12 Team</h1>
</header>
<main>
<nav id="nav-menu">
<a href="#" onclick="changeActiveSection('Welcome')">Home</a>
<button class="drop-button">About Us &#x25BC </button>
<div class="dropdown-content" style="display: none;">
<a href="#" onclick="changeActiveSection('Stanton')">Stanton</a>
<a href="#" onclick="changeActiveSection('Jenna')">Jenna</a>
<a href="#" onclick="changeActiveSection('Cooper')">Cooper</a>
</div>
<button class="drop-button">Profile &#x25BC;</button>
<div class="dropdown-content">
<a href="#">Test</a>
</div>
<a href="#" onclick="changeActiveSection('IT Work')">IT Work</a>
<a href="#" onclick="changeActiveSection('Industry Data')">Industry Data</a>
<a href="#" onclick="changeActiveSection('Our Project')">Our Project</a>
<a href="#" onclick="changeActiveSection('IT Technologies')">IT Technologies</a>

</nav>
<div id="Welcome" class="body-panel" style="display: none;">
<h2> </h2>
<p> Please select an option from the menu. </p>
</div>
<div id="Stanton" class="body-panel" style="display: none;">
<h2> this is a placeholder heading </h2>
<p> this is some placeholder body text. </p>
<p> Stanton </p>
</div>
<div id="Jenna" class="body-panel" style="display: none;">
<h2> this is a placeholder heading </h2>
<p> this is some placeholder body text. </p>
<p> Jenna </p>
</div>
<div id="Cooper" class="body-panel" style="display: none;">
<h2> this is a placeholder heading </h2>
<p> this is some placeholder body text. </p>
<p> Cooper </p>
</div>
</main>
<footer>

</footer>
<script>
//JS from snippet goes here
</script>
</body>

</html>

最佳答案

let sections = ["Cooper", "Jenna", "Lyly", "Mick", "Samuel", "Stanton", "Welcome"];
var visSection = null;

var i;
for (i = 0; i < sections.length; i++) {
console.log(sections[i]);
}

function changeActiveSection(sectionID) {
if (visSection === sectionID) {
visSection = null;
} else {
visSection = sectionID;
}
hideSections(visSection);
}

function hideSections(sectionID2) {
var i, targetSection, headerText;
for(i = 0; i < sections.length; i++) {
targetSection = sections[i];
section = document.getElementById(targetSection);
if(visSection === targetSection) {
console.log(visSection, targetSection, section);
section.style.display = "block";
populateHeader(visSection); // moved to here as per Andreas point
} else {

if (typeof(section) != 'undefined' && section != null){
section.style.display = "none";
}

}
//populateHeader(headerText); <- move as per Andreas point
}
}

function populateHeader(headerText) {
var ob;
ob = document.getElementById("header-content");
ob.innerText = headerText;
}

var dropdown = document.getElementsByTagName("button");
var i, objCol;

for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function() {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
});
}
/*Sets the font family for entire page.
removes entire margin for body section to create borderless display.*/

body {
font-family: Arial, Veranda, Serif;
margin: 0;
}


/*Sets attributes for the nav menu root element*/

#nav-menu {
height: 100%;
width: 15%;
position: fixed;
top: 0;
left: 0;
background-color: #353940;
overflow-x: hidden;
text-decoration: none;
text-align: center;
z-index: 1;
}


/*specifies hyperlink button text attributes*/

#nav-menu a {
text-decoration: none;
color: white;
font-size: 12pt;
display: block;
border-top: 1px solid white;
overflow: visible;
height: 20px;
padding-top: 5px;
padding-bottom: 5px;
}


/*Specifies most dropdown button attributes.*/

button.drop-button {
text-decoration: none;
background-color: #353940;
border: 0;
width: 100%;
color: white;
height: 20px;
outline: none;
font-size: 12pt;
border-top: 1px solid white;
overflow: visible;
padding-top: 5px;
padding-bottom: 5px;
}


/*Active class to highlight activated dropdown buttons*/

button.active {
background: #230fa8;
}


/*container class for dropdown menu items - hidden by default (display is manupulated via JS)*/

.dropdown-content {
display: none;
background-color: #4a5059;
}


/*specifies layout for dropdown menu items*/

.dropdown-content a {
display: block;
padding-top: 5px;
}


/*keeps header in line with rest of body*/

#header-content {
padding-left: 17%;
}


/*specifies positioning for body container divs*/

.body-panel {
position: absolute;
padding-left: 17%;
width: 75%;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" meta charset="utf-8" />
<title>Assignment 2 Group 12</title>
<link rel="stylesheet" href="css/style.css">
</head>

<body>
<header>
<h1 id="header-content" style="display: block;">Welcome to The A2-Group-12 Team</h1>
</header>
<main>
<nav id="nav-menu">
<a href="#" onclick="changeActiveSection('Welcome')">Home</a>
<button class="drop-button">About Us &#x25BC </button>
<div class="dropdown-content" style="display: none;">
<a href="#" onclick="changeActiveSection('Stanton')">Stanton</a>
<a href="#" onclick="changeActiveSection('Jenna')">Jenna</a>
<a href="#" onclick="changeActiveSection('Cooper')">Cooper</a>
</div>
<button class="drop-button">Profile &#x25BC;</button>
<div class="dropdown-content">
<a href="#">Test</a>
</div>
<a href="#" onclick="changeActiveSection('IT Work')">IT Work</a>
<a href="#" onclick="changeActiveSection('Industry Data')">Industry Data</a>
<a href="#" onclick="changeActiveSection('Our Project')">Our Project</a>
<a href="#" onclick="changeActiveSection('IT Technologies')">IT Technologies</a>

</nav>
<div id="Welcome" class="body-panel" style="display: none;">
<h2> </h2>
<p> Please select an option from the menu. </p>
</div>
<div id="Stanton" class="body-panel" style="display: none;">
<h2> this is a placeholder heading </h2>
<p> this is some placeholder body text. </p>
<p> Stanton </p>
</div>
<div id="Jenna" class="body-panel" style="display: none;">
<h2> this is a placeholder heading </h2>
<p> this is some placeholder body text. </p>
<p> Jenna </p>
</div>
<div id="Cooper" class="body-panel" style="display: none;">
<h2> this is a placeholder heading </h2>
<p> this is some placeholder body text. </p>
<p> Cooper </p>
</div>
</main>
<footer>

</footer>
<script>
//JS from snippet goes here
</script>
</body>

</html>

关于javascript - 为什么 JavaScript 将元素写入控制台但不通过函数更新 css 显示值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58339840/

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