HTML表格内的JavaScript循环无效

时间:2012-09-05 08:34:23

标签: javascript

我有一个JavaScript分配,其中一部分是一个带有嵌入式JavaScript的双列表,它在Celsius和Fahrenheit中循环50个温度。

我检查了W3的HTML验证器,它告诉我到目前为止我有2个错误......我似乎永远无法摆脱的是“元素脚本不允许作为元素tbody的子元素在此上下文中”。

这是我的代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<style type="text/css">
body { background-image:url('background.png'); text-align:center; font-family:verdana; color:white; }

ul
{
list-style-type:none;
margin:0;
padding:0;
padding-top:6px;
padding-bottom:6px;
}
li
{
display:inline;
}

a:link,a:visited
{
opacity:0.75;
filter:alpha(opacity=75);
font-weight:bold;
color:#FFFFFF;
background-color:#999999;
text-align:center;
padding:5px;
text-decoration:none;
text-transform:uppercase;
border: 1px solid white;
font-weight:bold;
}

a:hover,a:active
{
opacity:1;
filter:alpha(opacity=100);
background-color:#999999;
border: 1px solid white;
font-weight:bold;
}

h1 { font-size: 25px; }
h2 { font-size: 12px; }
h3 { font-size: 10px; position:fixed; bottom: 5px; left:0%; right:0%; font-weight:normal; }
h4 { font-size: 25px; text-transform:uppercase; }

navigationtext { font-weight:bold; text-transform:uppercase; }

</style>
<head>
<title>Untitled Document</title>
</head>
<body>
<table>
<tr><td>Celsius</td><td>Fahrenheit</td></tr>
<script type="text/javascript" src="temperatures.js"></script>
</table>
</body>
</html>

这是外部JavaScript文件:

var celsius = 0;
for (celsius = 0; celsius <= 50; celsius) {
    document.write("<tr><td>"+celsius+"</td><td>"+((celsius*9/5)+32)+"</td</tr>");
    celsius++;
}

3 个答案:

答案 0 :(得分:1)

首先,将script放在body的底部,就在</body>之前。将它放在其他地方会阻止页面加载。通过为要操作的元素提供ID来调整HTML以进行正确的DOM操作:

<table>
    <thead>
        <tr><th>Celsius</th><th>Fahrenheit</th></tr>
    </thead>
    <tbody id="temperatureTableTbody"></tbody>
</table>
<script type="text/javascript" src="temperatures.js"></script>
</body>

还要使闭包不要用不必要的变量污染全局范围。我稍微修改了你的代码以使用正确的DOM操作,应该适用于过去十年发布的任何浏览器。

(function() { //creates a closure, so vars don't leak in the global scope
    //stores a reference to the tbody DOM element which we will append rows to
    var tbody = document.getElementById('temperatureTableTbody');
    //iterates celsius from 0 to 50
    for (var celsius = 0; celsius <= 50; celsius++) {
        //creates elements
        var tr = document.createElement('tr'),
            tdcelsius = document.createElement('td'),
            tdfahr = document.createElement('td');
        //append textNodes to the td elements
        tdcelsius.appendChild(document.createTextNode(celsius));
        tdfahr.appendChild(document.createTextNode((celsius*9/5)+32));
        //appends the tds to the tr
        tr.appendChild(tdcelsius);
        tr.appendChild(tdfahr);
        //append the finished row to the tbody
        tbody.appendChild(tr);
        //repeat loop while condition is true
    }
}());​ //closes the Immediately Invoked Function Expression

Fiddle

请不要使用document.write

一些参考:Why is document.write considered a 'bad practice'?

答案 1 :(得分:0)

试试这样。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<body>
<table>
<tr>
    <td>Celsius</td>
    <td>Fahrenheit</td>
</tr>
<script type="text/javascript" > //<![CDATA[
    for (var celsius = 0; celsius <= 50; celsius++) {
        document.write("<tr><td>"+ celsius +"</td>");
        document.write("<td>"+ ((celsius*9/5)+32) +"</td></tr>");
}
//]]></script>
</table>
</body>
</html>

答案 2 :(得分:0)

您应该在加载文档时调用javascript函数:

if (document.readyState === "complete") { loadTemperatures(); }

您的loadTemperatures函数会将行追加到您的表中。看看这里学习如何做到这一点: http://viralpatel.net/blogs/dynamically-add-remove-rows-in-html-table-using-javascript/

  

在此上下文中,元素脚本不允许作为元素tbody的子元素

<script><head>之间加上</head>反复无常的错误。

这是一个向您展示目标的jsFiddle:http://jsfiddle.net/dfTz6/2/