我需要为每个tr设置单独的类名
<!DOCTYPE html>
<html>
<head>`enter code here`
<title>Json Table</title>
<link rel = "icon" href = "json_logo.png">
<style>
body{
width: 100%;
height: 100%;
}
div.index.body{
width: 14%;
height: 40%;
margin:5% 0% 0% 40%;
}
h1{
font-size: 18px;
padding-inline-start: 7%;
}
table,td{
border:1px solid grey;
border-collapse: collapse;
padding: 8px 16px;
}
table{
border-radius: 5px;
}
input[type=button]{
cursor: pointer;
margin: 1% 0.5% 0.5% 3.5em;
padding: 0.4% 0.7% 0.4% 0.7%;
border-radius: 3px;
text-decoration: none;
font-size: 20px;
transition: .3s;
-webkit-transition: .3s;
-moz-transition: .3s;
-o-transition: .3s;
color: white;
background-color: #616161;
border: 2px #616161 solid;
display: inline-block;
outline: none;
}
input[type=button]:hover{
color: #616161;
background-color: #fff;
border: 2px #616161 solid;
}
table > tr:nth-of-type(1){
color: white;
background-color: #616161;
text-align: center;
font-weight: bold;
font-size: 21px;
}
img{
width:100px;
height:56px;
padding-inline-start:23%;
}
</style>
</head>
<body>
<div class="index body">
<h1>Printing json key and value</h1>
<img src="clickme.gif">
<input type="button" id="json_table" value="Click Here" />
<p id="showData"></p>
</div>
</body>
<script>
// default table
var myJson = {"car":"car model","Ferrari":"Ferrari 488", "Lamborghini":"Aventador", "Bugatti":"La Voiture Noire"};
var table = document.createElement('table');
for (var key in myJson) {
var tr = document.createElement('tr');
var keyTd = document.createElement('td');
keyTd.textContent = key;
var valueTd = document.createElement('td');
valueTd.textContent = myJson[key];
tr.appendChild(keyTd);
tr.appendChild(valueTd);
table.appendChild(tr);
}
document.getElementById("showData").appendChild(table);
//table 1
var myFerrari = {"Engine":"V8 3,9 l","Induction":"Turbocharged", "Weight":"1,475 kg (3,252 lb)", "0-100 km/h":"3.0 s"};
var table = document.createElement('table');
for (var key in myFerrari) {
var tr = document.createElement('tr');
var keyTd = document.createElement('td');
keyTd.textContent = key;
keyTd.getElementById("key").className = "mystyle";
var valueTd = document.createElement('td');
valueTd.textContent = myFerrari[key];
tr.appendChild(keyTd);
tr.appendChild(valueTd);
table.appendChild(tr);
}
document.getElementById("showData").appendChild(table);
// table 2
var myLamborghini = {"Horsepower":"515 to 544 kW","Engine":"6.5 L V12", "Curb weight":"1,575 to 1,625 kg", "0-100 km/h":"2.9 sec"};
var table = document.createElement('table');
for (var key in myLamborghini) {
var tr = document.createElement('tr');
var keyTd = document.createElement('td');
keyTd.textContent = key;
var valueTd = document.createElement('td');
valueTd.textContent = myLamborghini[key];
tr.appendChild(keyTd);
tr.appendChild(valueTd);
table.appendChild(tr);
}
document.getElementById("showData").appendChild(table);
//table 3
var myBugatti = {"Top speed":"420 kph / 261 mph","Power & torque":"1500 hp / 1103 kW", "Weight":"2000 kg / 4409 lbs", "0-100 km/h":"2.4 s"};
var table = document.createElement('table');
for (var key in myLamborghini) {
var tr = document.createElement('tr');
var keyTd = document.createElement('td');
keyTd.textContent = key;
var valueTd = document.createElement('td');
valueTd.textContent = myLamborghini[key];
tr.appendChild(keyTd);
tr.appendChild(valueTd);
table.appendChild(tr);
}
document.getElementById("showData").appendChild(table);
</script>
</html>