I am trying to create a dynamic html table through JavaScript. I need to hide the teamid td (cell1). Need to set display as none. How can I do this?
function generate_table()
{
var body = document.getElementsByTagName("body")[0];
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
var teamrecord = "test";
for (var i = 0; i < teamrecord.length; i++) {
var row = document.createElement("tr");
var cell = document.createElement("td");
var cell1 = document.createElement("td");
var cell2 = document.createElement("td");
var cellText = document.createTextNode("teamrecord");
var cellId = document.createTextNode("teamid");
var radio = document.createElement("INPUT");
radio.setAttribute("type", "radio");
radio.setAttribute("name", "radio");
cell.appendChild(cellText);
cell1.appendChild(cellId);
cell2.appendChild(radio);
row.appendChild(cell);
row.appendChild(cell1);
row.appendChild(cell2);
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
body.appendChild(tbl);
}
I use the following code but its not working
cell1.setAttribute("display","none");
像这样:
cell1.style.display = "none";
Just set the style display
of required cell to none
.
Add cell1.style.display = 'none';
to your code.
Working code below:
function generate_table() { var body = document.getElementsByTagName("body")[0]; var tbl = document.createElement("table"); var tblBody = document.createElement("tbody"); var teamrecord = "test"; for (var i = 0; i < teamrecord.length; i++) { var row = document.createElement("tr"); var cell = document.createElement("td"); var cell1 = document.createElement("td"); var cell2 = document.createElement("td"); var cellText = document.createTextNode("teamrecord"); var cellId = document.createTextNode("teamid"); var radio = document.createElement("INPUT"); radio.setAttribute("type", "radio"); radio.setAttribute("name", "radio"); cell.appendChild(cellText); cell1.appendChild(cellId); cell2.appendChild(radio); cell1.style.display = 'none'; row.appendChild(cell); row.appendChild(cell1); row.appendChild(cell2); tblBody.appendChild(row); } tbl.appendChild(tblBody); body.appendChild(tbl); } generate_table();
You need to define it as a style like this:
cell1.style.display = 'none';
尝试使用cell1.style.visibility = "hidden"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.