gpt4 book ai didi

javascript - 将 JS 值插入 HTML 表

转载 作者:行者123 更新时间:2023-11-28 01:32:11 26 4
gpt4 key购买 nike

我有一个检索浏览器详细信息(版本、浏览器、操作系统等)的脚本,但我只是使用为此示例启用的 cookie。我希望能够将我的结果分成一个包含 2 列的表,但我的问题是任何人都可以帮助我如何让 JS 将结果插入第二列?

HTML:

 <body>
<table>
<tr>
<td>Cookies Enabled:</td>
<td id="Cookie"></td>
</tr>
</table>
<body>

Javascript:

 //Cookies enabled
var Cookies;
{
if (navigator.cookieEnabled) {
Cookies = "True";
} else {
Cookies = "False";
}
}
var Cookies = Cookie;
window.onload = function () {
document.getElementById("Cookie").innerHTML = Cookies;

//Cookies enabled   
var Cookies; {
if (navigator.cookieEnabled) {
Cookies = "True";
} else {
Cookies = "False";
}
}
var Cookies = Cookie;
window.onload = function() {
document.getElementById("Cookie").innerHTML = Cookies;
<body>
<table>
<tr>
<td>Cookies Enabled:</td>
<td id="Cookie"></td>
</tr>
</table>

<body>

预期结果:

   Cookies Enabled:      True
Operating System: Windows
Javascript Enabled: True
etc.

我已经编写了用于获取值的脚本,我只需要帮助将它们插入到表中。谢谢

最佳答案

首先,为什么您的答案不起作用:

var Cookies;

{
if (navigator.cookieEnabled) {
Cookies = "True";
} else {
Cookies = "False";
}
}

// you're over-writing your found Cookies variable with
// the value of Cookie, which you haven't assigned and
// is therefore retrieved from the browser, in most (perhaps all)
// browsers if an element with this id exists (as it does, here)
// the uninitialised variable will be a reference to that Node
// otherwise (if not initialised) it'll be undefined.
var Cookies = Cookie;

window.onload = function() {
// here you're correctly attempting to assign the variable,
// but it's the wrong variable:
document.getElementById("Cookie").innerHTML = Cookies;

// here you should be, but aren't, closing the assignment of the
// window.onload function.

您更正后的代码(删除(显然)不必要的花括号、不必要的变量赋值并正确关闭函数:

//Cookies enabled   
var Cookies;

if (navigator.cookieEnabled) {
Cookies = "True";
} else {
Cookies = "False";
}

window.onload = function() {
document.getElementById("Cookie").innerHTML = Cookies;
};
<table>
<tr>
<td>Cookies Enabled:</td>
<td id="Cookie"></td>
</tr>
</table>

//Cookies enabled

window.onload = function() {
document.getElementById("Cookie").innerHTML = navigator.cookieEnabled;
};
<table>
<tr>
<td>Cookies Enabled:</td>
<td id="Cookie"></td>
</tr>
</table>

关于javascript - 将 JS 值插入 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29984446/

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