简体   繁体   中英

Can I put a script tag inside a table?

I'm currently in college taking a webdesign class and we are working with javascript. We have an assignment to create a table that gives us random numbers inside using <script> and <table> . My question is where to put the tag?

I've tried using quotes around the <td> tags but it still isn't loading on my site.

<body>
<table border=1>
<tr>
    <script>
    '<td></td>'
    '<td>d</td>'
    '<td>d</td>'
</script>

The table I'm supposed to be making has 3 columns and 10 rows with 10 random variables from 50-100 in one column.

edit: Here is exactly what I have to do for the assignment:

All the numbers are random Create code to make a table with a border inside the table • create at least 1 function to call 10 random variables from 50-100 • use if/else statements • post a pic how you feel about each letter grade outside the table • average the total score for all 10 grades and tell me the average score

Use the DOM:

 document.getElementById("1").innerHTML = Math.random(); document.getElementById("a").innerHTML = Math.random();
 <table border="1"> <tr> <td id="a"></td><td id = "1"></td> </tr> </table>

This, of course, can be massively improved upon. You could use a JS loop to iterate through the elements. Definitely do not use my code char for char. The idea behind using 1 character ids of both letters and numbers is to demonstrate the versatility of the DOM.

As per the OP's comment, you would end up with a webpage looking something like this:

<!DOCTYPE html>
<html>
    <body>
        <table border = "1">
            <tr><td id = "a"></td><td id = "1"></td></tr>
        </table>
        <script>
            document.getElementById("1").innerHTML = Math.random();
            document.getElementById("a").innerHTML = Math.random();
        </script>
    </body>
</html>
<html>
    <body>
        <table>
            <tbody id="mybody"></tbody>

        </table>

        <script>
            // script after element rendered on page
            let b = document.getElementById('mybody');
            let tr = '<tr>' +
                '<td>' + parseInt( Math.random()  * 1000) + '</td>' +
                '<td>' + parseInt( Math.random()  * 100) + '</td>' +
                '<td>' + parseInt( Math.random()  * 10) + '</td>' +
                '</tr>';

            b.innerHTML = tr;   // add content to body of table

        </script>

    </body>

</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM