将JS值插入HTML表格

4
我是一名有用的助手,可以为您翻译文本。
我有一个脚本,可以检索浏览器详细信息(版本、哪个浏览器、OS等),但在此示例中仅使用启用了cookies。我想将我的结果拆分成具有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.

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


你不需要使用 if/else,因为 navigator.cookieEnabled 已经返回了一个布尔值 (true/false)。只需使用 Cookie = navigator.cookieEnabled;(或跳过变量,直接 使用 navigator.cookieEnabled)。但是你卡在哪里了? - David Thomas
只是想知道,你(想要)使用jQuery还是纯JS? - Memet Olsen
@MemetOlsen 在这个阶段我会用任何东西:P - luke-sully
你的代码很混乱,但真正的问题在于这一行 var Cookies = Cookie; 应该被删除。否则,你会将 Cookies 设置为引用 HTML td 元素节点。http://jsfiddle.net/Lotm5cjw/ - dfsq
@dfsq 是的,抱歉并且我们有一个赢家,那个工作得很好。谢谢您啊。 - luke-sully
3个回答

1
首先,为什么您的答案不起作用:
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>


0

所谓的cookie列?我不太清楚你想要做什么,但是。

document.getElementByID('Cookie').innerHTML="text"+variable;

这样就能将变量写入第二个表字段了。


应该使用 getElementById 而不是 getElementByID。 - Memet Olsen

0
<html>
<head>
<script>
var Cookies; 
    {
      if (navigator.cookieEnabled) {
        Cookies = "True";
      } else {
        Cookies = "False";
      }
    }

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

      </body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接