1

I'm making a page that should display the variable of my choosing within a table. I did the same thing twice, changed the id's and cuch, but only one works. I spent an hour looking over the syntax, and redoing allot of the work but it just doesn't help. Here's the three main pieces of code:

var NewGlad = new Array("Jokomopo","Etony","Roy");
var BestGlad = new Array("Johnny","Cod","Billy");

function UpdateTable(){

    document.getElementById("0New").innerHTML = "<a href='" +NewGlad[0]+ "'>" + NewGlad[0] + "</a>";


    document.getElementById("0Best").innerHTML = "<a href='" +BestGlad[0]+ "'>" + BestGlad[0] + "</a>";

}

//The function above IS executed onLoad.

Here's the html, I'll just show you the table row we're dealing with:

<tr>

<td class="NewestGlads" id="0New">
fgggrf
</td>

<td class="BestGlads" id="0Best">
fgggrf
</td>

</tr>

Only the NewestGlads td is updated. The other still displays "fgggrf"

4

3 回答 3

2

Don't say this should be easy. If it were easy you would not be asking us for the answer. You code could use some refinement:

var UpdateTable = function () {
        "use strict";
        var NewGlad = ["Jokomopo", "Etony", "Roy"],
            BestGlad = ["Johnny", "Cod", "Billy"];
        document.getElementById("New0").innerHTML = "<a href='" + NewGlad[0] + "'>" + NewGlad[0] + "</a>";
        document.getElementById("Best0").innerHTML = "<a href='" + BestGlad[0] + "'>" + BestGlad[0] + "</a>";
    };

I changed your references so that they are starting with an alpha character instead of a number. Your HTML will need to change to match. I have never seen identifiers starting with a number and believe it to be a bad convention that could be error prone cross browser. Try this instead by calling the UpdateTable function. You can also execute this without a call by making the code as follows:

var UpdateTable = (function () {
        "use strict";
        var NewGlad = ["Jokomopo", "Etony", "Roy"],
            BestGlad = ["Johnny", "Cod", "Billy"];
        document.getElementById("New0").innerHTML = "<a href='" + NewGlad[0] + "'>" + NewGlad[0] + "</a>";
        document.getElementById("Best0").innerHTML = "<a href='" + BestGlad[0] + "'>" + BestGlad[0] + "</a>";
    }());
于 2012-04-12T03:38:07.377 回答
0

I just used your code, and this gave me the correct result you are going for

<html>
<head>
<script>
    var NewGlad = new Array("Jokomopo","Etony","Roy");
    var BestGlad = new Array("Johnny","Cod","Billy");

    function UpdateTable(){
        document.getElementById("0New").innerHTML = "<a href='" +NewGlad[0]+ "'>" + NewGlad[0] + "</a>";
        document.getElementById("0Best").innerHTML = "<a href='" +BestGlad[0]+ "'>" + BestGlad[0] + "</a>";

    }
</script>
</head>
<body onload="UpdateTable();">
<table>
<tr>
    <td class="NewestGlads" id="0New">fgggrf</td>
    <td class="BestGlads" id="0Best">fgggrf</td>
</tr>
</table>
</body>
</html>
于 2012-04-12T03:42:50.097 回答
-1

The innerHTML is incorrect.

Should read

document.getElementById("0New").innerHTML = "<a href=\"" +NewGlad[0]+ "\">" + NewGlad[0] + "</a>"; 
document.getElementById("0Best").innerHTML = "<a href=\"" +BestGlad[0]+ "\">" + BestGlad[0] + "</a>"; 

I.e double quotes around the href.

Also the IDs should start with a letter. Change them to New0and Best0

于 2012-04-12T03:42:17.300 回答