将JS值插入HTML表

时间:2015-05-01 09:27:54

标签: javascript html

我有一个脚本可以检索浏览器详细信息(版本,浏览器,操作系统等),但我只是在本例中使用了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.

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

3 个答案:

答案 0 :(得分: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.

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

&#13;
&#13;
//Cookies enabled   
var Cookies;

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

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

&#13;
&#13;
//Cookies enabled

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

答案 1 :(得分:0)

列名为cookie? 我不清楚你想要做什么,但是。

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

这应该能够将变量写入第二个表字段。

答案 2 :(得分: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>
相关问题