gpt4 book ai didi

javascript - document.getElementById.innerHTML 没有写任何内容..?

转载 作者:行者123 更新时间:2023-12-03 11:36:53 26 4
gpt4 key购买 nike

我的问题:

document.getElementById("values").innerHTML 不写入任何内容。如果我尝试执行 document.getElementById("values").innerHTML = "stuff"; (仅使用字符串) - 什么也不会发生。

我在这里做错了什么?

HTML:

<form onsubmit="save_entry();return false;">
<label for="i_km">Kilometer: <input type="text" name="km" id="i_km"></label><br>
<label for="i_fuel">Sprit: <input type="text" name="fuel" id="i_fuel"></label><br>
<input type="submit" value="Save" />
</form>
<div id="values"></div>

JavaScript:

function save_entry() {
var anzahl = localStorage.length/2;
var nameKeyKm = "k" + anzahl;
localStorage.setItem(nameKeyKm,document.forms[0]["km"].value);
var nameKeyF = "F" + anzahl;
localStorage.setItem(nameKeyF,document.forms[0]["fuel"].value);
document.write("Entry saved!")
}

function show_entry() {
document.getElementById("values").innerHTML = "<table><th>Kilometers</th><th>Tanked</th>";
for (var i = 0; i < localStorage.length/2; i++) {
alert("d");
var temp_km = "k"+i;
var temp_f = "F"+i;
document.getElementById("values").innerHTML = "<tr>";
document.getElementById("values").innerHTML = "<td>"+localStorage.getItem(temp_km)+"</td>";
document.getElementById("values").innerHTML = "<td>"+localStorage.getItem(temp_f)+"</td>";
document.getElementById("values").innerHTML = "</tr>";
}
document.getElementById("values").innerHTML = "</table>";
}
show_entry();

最佳答案

这确实有效!

function show_entry(){
var content = '';
content = content + '<table><th>Kilometer</th><th>Getankt</th>';

for(var i = 0; i < localStorage.length/2; i++)
{
var temp_km = "k"+i;
var temp_f = "F"+i;
content = content + "<tr>";
content = content + "<td>"+localStorage.getItem(temp_km)+"</td>";
content = content + "<td>"+localStorage.getItem(temp_f)+"</td>";
content = content + "</tr>";
}
content = content + "</table>";

document.getElementById("values").innerHTML = content;
}

关于javascript - document.getElementById.innerHTML 没有写任何内容..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26454049/

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