即使点击,计数器也会永久显示
Counters show permanently even if onclick
我在 table 中有以下条目:
<tr>
<td class="right-middle user">Name</td>
<td class="right-middle done">
<div onload="clickCounter()" id="result"></div>
</td>
<td class="right-middle check">
<img src="img/check.png" onclick="clickCounter()">
</td>
<td class="right-middle undone">
<div onload="addcounter()" id="result2"></div>
</td>
<td class="right-middle uncheck">
<img src="img/uncheck.png" onclick="addCounter()">
</td>
</tr>
以及以下 javascript:
function clickCounter() {
if (typeof (Storage) !== "undefined") {
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = localStorage.clickcount;
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
}
}
document.getElementById("result").innerHTML = localStorage.clickcount;
function addCounter() {
if (typeof (Storage) !== "undefined") {
if (localStorage.clickcounts) {
localStorage.clickcounts = Number(localStorage.clickcounts) + 1;
} else {
localStorage.clickcounts = 1;
}
document.getElementById("result2").innerHTML = localStorage.clickcounts;
} else {
document.getElementById("result2").innerHTML = "Sorry, your browser does not support web storage...";
}
}
document.getElementById("result2").innerHTML = localStorage.clickcounts;
我希望两个计数器始终显示在屏幕上,即使刷新页面后也不必按按钮查看值。我已经使用了 table 中的这段代码,它们工作正常。谁能帮我理解为什么计数器不一直显示在屏幕上?
Div 个元素没有 load
个事件。只有从其他 URL(如图像和框架)和文档本身获取内容的元素才可以。
由于您使用的是内在事件属性([尽管它们存在问题](
)), 如果你使用了 a validator.
就会被拾取
或者:
- 只需 运行 脚本元素中出现在您要与之交互的元素之后的函数 或
- 使用文档的加载事件
onload()
事件不能用于任何其他元素,只能用于一组有限的标签:<body>
<frame>
<frameset>
<iframe>
<img>
<link>
<script>
我在 table 中有以下条目:
<tr>
<td class="right-middle user">Name</td>
<td class="right-middle done">
<div onload="clickCounter()" id="result"></div>
</td>
<td class="right-middle check">
<img src="img/check.png" onclick="clickCounter()">
</td>
<td class="right-middle undone">
<div onload="addcounter()" id="result2"></div>
</td>
<td class="right-middle uncheck">
<img src="img/uncheck.png" onclick="addCounter()">
</td>
</tr>
以及以下 javascript:
function clickCounter() {
if (typeof (Storage) !== "undefined") {
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = localStorage.clickcount;
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
}
}
document.getElementById("result").innerHTML = localStorage.clickcount;
function addCounter() {
if (typeof (Storage) !== "undefined") {
if (localStorage.clickcounts) {
localStorage.clickcounts = Number(localStorage.clickcounts) + 1;
} else {
localStorage.clickcounts = 1;
}
document.getElementById("result2").innerHTML = localStorage.clickcounts;
} else {
document.getElementById("result2").innerHTML = "Sorry, your browser does not support web storage...";
}
}
document.getElementById("result2").innerHTML = localStorage.clickcounts;
我希望两个计数器始终显示在屏幕上,即使刷新页面后也不必按按钮查看值。我已经使用了 table 中的这段代码,它们工作正常。谁能帮我理解为什么计数器不一直显示在屏幕上?
Div 个元素没有 load
个事件。只有从其他 URL(如图像和框架)和文档本身获取内容的元素才可以。
由于您使用的是内在事件属性([尽管它们存在问题](
或者:
- 只需 运行 脚本元素中出现在您要与之交互的元素之后的函数 或
- 使用文档的加载事件
onload()
事件不能用于任何其他元素,只能用于一组有限的标签:<body>
<frame>
<frameset>
<iframe>
<img>
<link>
<script>