最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating Tables Using Java Script - Stack Overflow

programmeradmin5浏览0评论

Im having an issue using Java script to create a table that displays an array. I must use an external Javascript file to create the array, and use a for loop to create and populate a table. The issue I am having is nothing is happening inside of the script tags. I can write "Document.write('Hellow World!')" and nothing happens. So not surprise when i try to create the for loop with the array, nothing happens. Any help would be appreciated. This is my first time using Javascript in this manner and the notes that professor provided hold no relevant information on how to go about creating a tab;e using a for-loop.

myarray.js

var classArray = new Array();
classArray[0] = "CST415 Software Enterprise II";
classArray[1] = "CST420 Foundations for Distruibuted Web Applications in Java"
classArray[2] = "CST433 Database Technology"
classArray[3] = "Ethics and Professional Computing"
classArray[4] = "Introdiction to Graphics Programming"

Suspect Code:

<table class="classes">
    <tr>
        <td>
            Classes Taken for Computer Science Major
        </td>
    </tr>
        <script type="text/javascript" src="myarray.js" >
            for(var i=0; i<5;i++){
                document.write('<tr><td>');
                document.write(classArray[i]);
                document.write('</td></tr>');

            }
        </script>
</table>

Im having an issue using Java script to create a table that displays an array. I must use an external Javascript file to create the array, and use a for loop to create and populate a table. The issue I am having is nothing is happening inside of the script tags. I can write "Document.write('Hellow World!')" and nothing happens. So not surprise when i try to create the for loop with the array, nothing happens. Any help would be appreciated. This is my first time using Javascript in this manner and the notes that professor provided hold no relevant information on how to go about creating a tab;e using a for-loop.

myarray.js

var classArray = new Array();
classArray[0] = "CST415 Software Enterprise II";
classArray[1] = "CST420 Foundations for Distruibuted Web Applications in Java"
classArray[2] = "CST433 Database Technology"
classArray[3] = "Ethics and Professional Computing"
classArray[4] = "Introdiction to Graphics Programming"

Suspect Code:

<table class="classes">
    <tr>
        <td>
            Classes Taken for Computer Science Major
        </td>
    </tr>
        <script type="text/javascript" src="myarray.js" >
            for(var i=0; i<5;i++){
                document.write('<tr><td>');
                document.write(classArray[i]);
                document.write('</td></tr>');

            }
        </script>
</table>
Share Improve this question asked Nov 16, 2011 at 18:52 merileymeriley 1,8813 gold badges20 silver badges33 bronze badges 2
  • 7 document.write is SOOOOoOOooo 1990s. Get with the modern age and use DOM operations instead. – Marc B Commented Nov 16, 2011 at 18:55
  • Also, Array literal notation is your best friend: classArray = ["CST415", "CST420"]; – hugomg Commented Nov 16, 2011 at 21:14
Add a ment  | 

6 Answers 6

Reset to default 3

You can't have a single <script> both load an external .js file (via the src= attribute) AND contain its own in-line code (your for loop). You must split this into two parts:

<script type="text/javascript" src="myarray.js"></script>
<script type="text/javascript">
   for (...) {
      ....
   }
</script>

For those ing in from Google and finding the other answers unsatisfying. Here's an introduction, without jQuery:

var table = document.createElement("table");
var tableBody = document.createElement("tbody");

var row = document.createElement("tr");

for (var i = 0; i < 5; i++) {
    var cell = document.createElement("td");
    var cellText = document.createTextNode("This is cell " + i);
    cell.appendChild(cellText);
    row.appendChild(cell);
}

tableBody.appendChild(row);

table.appendChild(tableBody);

Try it in jsFiddle here.

You can not have inline script inside of an external script tag.

They need to be two separate tags.

I'd rather refactor your code to:

<script type="text/javascript">
var myTable = document.getElementById('my_table');
// use array literals, not new Array()
var elements = [
    "CST415 Software Enterprise II",
    "CST420 Foundations for Distruibuted Web Applications in Java",
    "CST433 Database Technology",
    "Ethics and Professional Computing",
    "Introdiction to Graphics Programming"
].map(function(title) {
     return '<tr><td>' + title + '</td></tr>'
});

document.getElementById('my_table').tBodies[0].innerHTML = elements.join('');
</script>

<table id="my_table" class="classes">
    <thead>
      <tr>
        <td>
            Classes Taken for Computer Science Major
        </td>
      </tr>
    </thead>
    <tbody>
    </tbody>
</table>

You can check it here: http://jsfiddle/a3X7A/.

Might not be the best way, but I acplish this with jQuery

<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="myarray.js" >

<script type = "text/javascript">
for (i=0; i<5; i++) {
   $("#TableName").append("<tr><td>" + classArray[i] + "</td></tr>");
   //or if your table doesn't have a name you can find by element or by class
   $(".classes").....
   $("table").....
}
</script>
__author__ = 'kunaln'

a=input("enter first value ")

g=(int)(a)*(int)(1)
print (g)

k=(int)(a)*(int)(2)
print (k)

u=(int)(a)*(int)(3)
print (u)

n=(int)(a)*(int)(4)
print (n)

b=(int)(a)*(int)(5)
print (b)

z=(int)(a)*(int)(6)
print (z)

x=(int)(a)*(int)(7)
print (x)

y=(int)(a)*(int)(8)
print (y)

p=(int)(a)*(int)(9)
print (p)

d=(int)(a)*(int)(10)
print (d)
发布评论

评论列表(0)

  1. 暂无评论