2

我是 JS 新手。我放入document.getElementById我的 JS 并尝试将该元素放入一个pid 中,但是当我单击按钮时,什么都没有显示。我在这里做错了什么?任何帮助是极大的赞赏。谢谢!

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GRL Example</title>
<script>
function class_roll() {
    // Car Classes
    var classes = ["B", "A", "S", "R3", "R2", "R1"],
        classToUse = classes[Math.floor(Math.random() * classes.length)];
    var elemClass = document.getElementById("croll");
}

function track_roll() {
    // Tracks
    var tracks = ["Clear Springs Circuit", "Festival North Circuit", "Beaumont Circuit", "Finley Dam Circuit", "Gladstone Circuit", "Clifton Valley Trail", "Gladstone Trail"],
        trackToUse = tracks[Math.floor(Math.random() * tracks.length)];
    var elemTrack = document.getElementById("troll");
}
</script>
</head>

<body>

<p id="croll">Some text here</p>
    <button onclick="class_roll();">Class Roll</button>

<p id="troll">Some text here</p>
    <button onclick="track_troll;">Track Roll</button>

</body>
</html>
4

2 回答 2

3

带第一个功能

function class_roll() {
    // Car Classes
    var classes = ["B", "A", "S", "R3", "R2", "R1"],
        classToUse = classes[Math.floor(Math.random() * classes.length)];
    var elemClass = document.getElementById("croll");
}

您没有执行任何操作,因此添加一些对您可见的操作。改成。

function class_roll() {
    // Car Classes
    var classes = ["B", "A", "S", "R3", "R2", "R1"],
        classToUse = classes[Math.floor(Math.random() * classes.length)];
    document.getElementById("croll").innerHTML = classToUse ;
}

带第二功能

function track_roll() {
    // Tracks
    var tracks = ["Clear Springs Circuit", "Festival North Circuit", "Beaumont Circuit", "Finley Dam Circuit", "Gladstone Circuit", "Clifton Valley Trail", "Gladstone Trail"],
        trackToUse = tracks[Math.floor(Math.random() * tracks.length)];
    var elemTrack = document.getElementById("troll");
}
  1. 函数名中有拼写错误意味着您调用的名称与定义名称不同。
  2. document.getElementById("troll").innerHTML = trackToUse ; 添加此项以随机查看所选选项。

这是修复的代码以供您帮助

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GRL Example</title>

</head>

<body>

<p id="croll">Some text here</p>
    <button onclick="class_roll();">Class Roll</button>

<p id="troll">Some text here</p>
    <button onclick="track_roll();">Track Roll</button>

</body>
<script>
function class_roll() {
    // Car Classes
    var classes = ["B", "A", "S", "R3", "R2", "R1"],
        classToUse = classes[Math.floor(Math.random() * classes.length)];
    document.getElementById("croll").innerHTML = classToUse ;
}

function track_roll() {

    var tracks = ['a' ,'a' ,'a' ,'a'];
        trackToUse = tracks[Math.floor(Math.random() * tracks.length)];
    document.getElementById("troll").innerHTML = trackToUse ;

}
</script>
</html>
于 2013-01-24T04:42:01.460 回答
2

如果您想更改元素的值,则需要在检索到它后对其进行处理。

例如:

function track_roll() {
    // Tracks
    var tracks = ["Clear Springs Circuit", "Festival North Circuit", "Beaumont Circuit", "Finley Dam Circuit", "Gladstone Circuit", "Clifton Valley Trail", "Gladstone Trail"],
        trackToUse = tracks[Math.floor(Math.random() * tracks.length)];
    var elemTrack = document.getElementById("troll");
    elemTrack.InnerHTML = "Test";
}
于 2013-01-24T04:19:38.957 回答