如何将JavaScript生成的div元素放置在主体div元素中



我尝试了几种不同的方法,将生成自己div元素的JavaScript生成的条形图放置到主div元素的主体中,但没有成功。有人知道如何做到这一点吗?

已编辑

这是CodePen,看看我在说什么。正如你所看到的,我有一个身体周围有边框的包装。然而,无论我把脚本放在哪里,我都无法将它放入包装器中。它总是在外部生成的。

任何帮助都将不胜感激。

这是代码。

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Issue Tracking System"/>
<meta name="author" content="Stephen Morris">
<link rel="stylesheet" type="text/css" href="tissue.css">
</head>
<body>
<div id="wrapper">
<h2>Test</h2>
<div class="topnav">
<a href="index.html">Home</a>
<a href="Login.html">Login</a>
</div>
<div>
<h2>Sales Subscription Dashboard</h2>
<script src="js/subscriptions_graph.js">
</div>
</div>
<div class="copyright">
Copyright &copy; 2018 
</div>
</body>
</script>
</html>

CSS

#wrapper {
margin-left: auto;
margin-right: auto;
width: 85%;
border: groove; 
border-color: white;
padding: 2px;
}
#loginwrap {
margin-left: auto;
margin-right: auto;
padding: 3px;
text-align: center;
}
body {
font-family: Georgia;
padding: 10px;
background: #f1f1f1;
font-weight: bold;
}

/* top navigation bar */
.topnav {
overflow: hidden;
background-color: #333;
}

/* topnav links */
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

/* Change color on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}

/* three columns next to each other */
.column1 {
float: left;
width: 30%;
padding: 15px;
height: 300px;
text-align: center;
background-color: #aaa;
}
.column2 {
float: left;
width: 30%;
padding: 15px;
height: 300px;
text-align: center;
background-color: #bbb;
}
.column3 {
float: left;
width: 30%;
padding: 15px;
height: 300px;
text-align: center;
background-color: #aaa;
}

/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}

/* Card-like background for each section */
.card {
background-color: white;
padding: 30px;
margin-top: 20px;
overflow: auto;
}

/* Align about section image to right */
.aboutimg {
float: right;
}

/* Footer */
.footer {
padding: 20px;
background: #ddd;
margin-top: 20px;
}
.copyright {
text-align: center;
font-size: 10px;
padding: 5px;
}

JavaScript

function createBarChart (data) {
// Start with the container.
var chart = document.createElement("div");
// The container must have position: relative.
chart.style.position = "relative";
// The chart's height is the value of its largest
// data item plus a little margin.
var height = 0;
for (var i = 0; i < data.length; i += 1) {
height = Math.max(height, data[i].value);
}
chart.style.height = (height + 10) + "px";
// Give the chart a bottom border.
chart.style.borderBottomStyle = "solid";
chart.style.borderBottomWidth = "1px";
// Iterate through the data.
var barPosition = 0;
// We have a preset bar width for the purposes of this
// example.  A full-blown chart module would make this
// customizable.
var barWidth = 25;
for (i = 0; i < data.length; i += 1) {
// Basic column setup.
var dataItem = data[i];
var bar = document.createElement("div");
bar.style.position = "absolute";
bar.style.left = barPosition + "px";
bar.style.width = barWidth + "px";
bar.style.backgroundColor = dataItem.color;
bar.style.height = dataItem.value + "px";
bar.style.borderStyle = "ridge";
bar.style.borderColor = dataItem.color;
// Visual flair with CSS Level 3 (for maximum compatibility
// we set multiple possible properties to the same value).
// Hardcoded values here just for illustration; a
// full module would allow major customizability.
bar.style.MozBoxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
bar.style.WebkitBoxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
bar.style.boxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
bar.style.MozBorderRadiusTopleft = "8px";
bar.style.WebkitBorderTopLeftRadius = "8px";
bar.style.borderTopLeftRadius = "8px";
bar.style.MozBorderRadiusTopright = "8px";
bar.style.WebkitBorderTopRightRadius = "8px";
bar.style.borderTopRightRadius = "8px";
bar.style.backgroundImage =
"-moz-linear-gradient(" + dataItem.color + ", black)";
bar.style.backgroundImage =
"-webkit-gradient(linear, 0% 0%, 0% 100%," +
"color-stop(0, " + dataItem.color + "), color-stop(1, black))";
bar.style.backgroundImage =
"linear-gradient(" + dataItem.color + ", black)";
// Recall that positioning properties are treated *relative*
// to the corresponding sides of the containing element.
bar.style.bottom = "-1px";
chart.appendChild(bar);
// Move to the next bar.  We provide an entire bar's
// width as space between columns.
barPosition += (barWidth * 2);
}
return chart;
};
window.onload = function () {
var colors = [{ color: "red", value: 40 },
{ color: "blue", value: 10 },
{ color: "green", value: 100 },
{ color: "black", value: 65 },
{ color: "yellow", value: 75 },
{ color: "purple", value: 120 },
{ color: "grey", value: 121 },
{ color: "orange", value: 175 },
{ color: "olive", value: 220 },
{ color: "maroon", value: 275 },
{ color: "brown", value: 300 },
{ color: "teal", value: 15 }
];
var chart = createBarChart(colors);
document.body.appendChild(chart);
};

您的问题是将它附加到正文中,从而使条形图开箱即用。

如果你换成这行,它将被放入#包装器:

document.body.appendChild(chart);

为此:

document.querySelector('#wrapper').appendChild(chart);

请注意,这在代码段的全屏模式中最为明显——当图形比较小屏幕上的包含包装器大时,您需要解决溢出问题。我在那里弹出了一个overflow-x: auto风格的规则,以显示它在包装器中。

此外,您没有正确关闭脚本标记,所以我也解决了这个问题。

function createBarChart (data) {
// Start with the container.
var chart = document.createElement("div");
// The container must have position: relative.
chart.style.position = "relative";
// The chart's height is the value of its largest
// data item plus a little margin.
var height = 0;
for (var i = 0; i < data.length; i += 1) {
height = Math.max(height, data[i].value);
}
chart.style.height = (height + 10) + "px";
// Give the chart a bottom border.
chart.style.borderBottomStyle = "solid";
chart.style.borderBottomWidth = "1px";
// Iterate through the data.
var barPosition = 0;
// We have a preset bar width for the purposes of this
// example.  A full-blown chart module would make this
// customizable.
var barWidth = 25;
for (i = 0; i < data.length; i += 1) {
// Basic column setup.
var dataItem = data[i];
var bar = document.createElement("div");
bar.style.position = "absolute";
bar.style.left = barPosition + "px";
bar.style.width = barWidth + "px";
bar.style.backgroundColor = dataItem.color;
bar.style.height = dataItem.value + "px";
bar.style.borderStyle = "ridge";
bar.style.borderColor = dataItem.color;
// Visual flair with CSS Level 3 (for maximum compatibility
// we set multiple possible properties to the same value).
// Hardcoded values here just for illustration; a
// full module would allow major customizability.
bar.style.MozBoxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
bar.style.WebkitBoxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
bar.style.boxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
bar.style.MozBorderRadiusTopleft = "8px";
bar.style.WebkitBorderTopLeftRadius = "8px";
bar.style.borderTopLeftRadius = "8px";
bar.style.MozBorderRadiusTopright = "8px";
bar.style.WebkitBorderTopRightRadius = "8px";
bar.style.borderTopRightRadius = "8px";
bar.style.backgroundImage =
"-moz-linear-gradient(" + dataItem.color + ", black)";
bar.style.backgroundImage =
"-webkit-gradient(linear, 0% 0%, 0% 100%," +
"color-stop(0, " + dataItem.color + "), color-stop(1, black))";
bar.style.backgroundImage =
"linear-gradient(" + dataItem.color + ", black)";
// Recall that positioning properties are treated *relative*
// to the corresponding sides of the containing element.
bar.style.bottom = "-1px";
chart.appendChild(bar);
// Move to the next bar.  We provide an entire bar's
// width as space between columns.
barPosition += (barWidth * 2);
}
return chart;
};
window.onload = function () {
var colors = [{ color: "red", value: 40 },
{ color: "blue", value: 10 },
{ color: "green", value: 100 },
{ color: "black", value: 65 },
{ color: "yellow", value: 75 },
{ color: "purple", value: 120 },
{ color: "grey", value: 121 },
{ color: "orange", value: 175 },
{ color: "olive", value: 220 },
{ color: "maroon", value: 275 },
{ color: "brown", value: 300 },
{ color: "teal", value: 15 }
];
var chart = createBarChart(colors);
document.querySelector("#wrapper").appendChild(chart); // I altered this line
};
#wrapper {
margin-left: auto;
margin-right: auto;
width: 100%;
border: groove; 
border-color: white;
padding: 2px;
overflow-x: auto;
}
#loginwrap {
margin-left: auto;
margin-right: auto;
padding: 3px;
text-align: center;
}
body {
font-family: Georgia;
padding: 10px;
background: #f1f1f1;
font-weight: bold;
}
/* top navigation bar */
.topnav {
overflow: hidden;
background-color: #333;
}
/* topnav links */
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change color on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* three columns next to each other */
.column1 {
float: left;
width: 30%;
padding: 15px;
height: 300px;
text-align: center;
background-color: #aaa;
}
.column2 {
float: left;
width: 30%;
padding: 15px;
height: 300px;
text-align: center;
background-color: #bbb;
}
.column3 {
float: left;
width: 30%;
padding: 15px;
height: 300px;
text-align: center;
background-color: #aaa;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Card-like background for each section */
.card {
background-color: white;
padding: 30px;
margin-top: 20px;
overflow: auto;
}
/* Align about section image to right */
.aboutimg {
float: right;
}
/* Footer */
.footer {
padding: 20px;
background: #ddd;
margin-top: 20px;
}
.copyright {
text-align: center;
font-size: 10px;
padding: 5px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Issue Tracking System"/>
<meta name="author" content="Stephen Morris">
<link rel="stylesheet" type="text/css" href="tissue.css">
</head>
<body>
<div id="wrapper">
<h2>Test</h2>
<div class="topnav">
<a href="index.html">Home</a>
<a href="Login.html">Login</a>
</div>
<div>
<h2>Sales Subscription Dashboard</h2>
</div>
</div>
<div class="copyright">
Copyright &copy; 2018 
</div>
<script src="js/subscriptions_graph.js"></script>
</body>
</html>

最新更新