0

我设计了一个使用 JAVASCRIPT + HTML5 的网页。它在 JSFIDDLE 中运行良好,但当我在记事本或 w3schools 站点中尝试时无法运行。我的代码有什么问题?

我的html代码是;

<head>
<style>
#scroll-content2  { height:100%; width:100%; background-color:blue;  }
.images  {  height:60px; width:60px;  }
.table2  {  width:300px;  }
.c2  {  height:300px; background-color:lightblue;   }
</style>
</head>
<body><section id="scroll-content2"></section></body>

我的 javascript 是;(我把它放在 html 文件的部分)

<script>
var a=['option a','option b','option c', 'option d','option e','option f'];
var b = ['good','ok','bad','excellent','average'];
function init() {
    function img() {
         var image = document.createElement('image');
     image.className = 'images';
    image.src = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQWUb6zXlz-7lTc53ACFi_kCg0Vojxha2Hk00602if3wOZ3my9j6A';
     }
    return image;   }
    window.onload = img()

for(var i=0;i<a.length;i++)  {
var ele = document.getElementById('scroll-content2');
var table = document.createElement('table');
table.className = 'table2';
var tr = document.createElement('tr');
tr.className = 'c2';
var td = document.createElement('td');
var txt4 = document.createTextNode(i+1 + "." +" ");
var txt = document.createTextNode(a[i]);
td.appendChild(txt4);td.appendChild(txt);

    for(var j=0;j<b.length;j++)
    {
           var divi = document.createElement('div');
       divi.className ='divi';
       var txt2 = document.createTextNode(b[j]);
          divi.appendChild(img());
      divi.appendChild(txt2);
      td.appendChild(divi); }
tr.appendChild(td);
table.appendChild(tr);
ele.appendChild(table);
}   }
window.onload = init()
</script>
4

1 回答 1

0

这有效:

var a = ['option a', 'option b', 'option c', 'option d', 'option e', 'option f'];
var b = ['good', 'ok', 'bad', 'excellent', 'average'];

function init() {
    for (var i = 0; i < a.length; i++) {
        var ele = document.getElementById('scroll-content2');
        var table = document.createElement('table');
        table.className = 'table2';
        var tr = document.createElement('tr');
        tr.className = 'c2';
        var td = document.createElement('td');
        var txt4 = document.createTextNode(i + 1 + "." + " ");
        var txt = document.createTextNode(a[i]);
        td.appendChild(txt4);
        td.appendChild(txt);

        for (var j = 0; j < b.length; j++) {
            var divi = document.createElement('div');
            divi.className = 'divi';
            var txt2 = document.createTextNode(b[j]);
            divi.appendChild(img());
            divi.appendChild(txt2);
            td.appendChild(divi);
        }
        tr.appendChild(td);
        table.appendChild(tr);
        ele.appendChild(table);
    }
}

function img() {
    var image = document.createElement('image');
    image.className = 'images';
    image.src = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQWUb6zXlz-7lTc53ACFi_kCg0Vojxha2Hk00602if3wOZ3my9j6A';
    return image;
}

window.onload = init();

JSFiddle

于 2013-07-13T08:24:24.850 回答