这是我的代码如何在div中插入java脚本函数的元素?
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
var year_table = document.createElement("TABLE");
year_table.setAttribute("id", "head_skill_table");
document.body.appendChild(year_table);
var year_tr = document.createElement("TR");
year_tr.setAttribute("id", "year_tr");
document.getElementById("head_skill_table").appendChild(year_tr);
for(i = 2006; i <= 2016; i++) {
var year_th = document.createElement("TH");
var year_cell = document.createTextNode(i);
year_th.appendChild(year_cell);
document.getElementById("year_tr").appendChild(year_th);
year_th.style.width = "25vw";
year_th.style.border = "2px dotted #a1a1a1";
year_th.style.borderRadius = "100%";
year_th.style.padding = "1.5% 0.5%";
for(j = 0; j < 4; j++) {
var year_hr = document.createElement("TH");
year_hr.innerHTML = '<hr style="border-top: 2px dotted #a1a1a1;">';
document.getElementById("year_tr").appendChild(year_hr);
year_hr.style.width = "25vw";
}
}
var year_th = document.createElement("TH");
var year_cell = document.createTextNode(2017);
year_th.appendChild(year_cell);
document.getElementById("year_tr").appendChild(year_th);
year_th.style.width = "25vw";
year_th.style.border = "2px dotted #a1a1a1";
year_th.style.borderRadius = "100%";
year_th.style.padding = "1.5% 0.5%";
}
</script>
</head>
<body >
<div id="one">
<!--Some Content-->
</div>
<div id="two">
<!--Some Content-->
</div>
<div id="three">
<!--Some Content-->
</div>
<div id="myDiv" style="some css style">
<!--Place my table created with javascript function here, so it will inherite the css style-->
</div>
<div id="four">
<!--Some Content-->
</div>
<div id="five">
<!--Some Content-->
</div>
<div id="six">
<!--Some Content-->
</div>
</body>
</html>
现在我想补充我的代码与ID在div =“myDiv”,使得得到由myFunction的()创建的表将继承myDiv的所有CSS样式() 我尝试了innerHTML,但没有让表格继承myDiv的风格。
您应该使用CSS样式表。 – SLaks
检查这个小提琴(https://jsfiddle.net/nrgbf14p/1/):添加div有适用的正确风格。但是一个div不会继承其父代的所有属性(请参阅'border'规则如何应用于匹配的div)。 – ValLeNain