[英]JavaScript and document.getElementById
I'm new to JS. 我是JS的新手。 I put
document.getElementById
in my JS and tried to put that element into a p
id, but when I click the button, nothing is showing up. 我把
document.getElementById
在我的JS,并试图把该元素为p
ID,但是当我点击该按钮,没有什么显示出来。 What am I doing wrong here? 我在这里做错了什么? Any help is greatly appreciated.
任何帮助是极大的赞赏。 Thank you!
谢谢!
<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>
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");
}
You are not performing any action therefore add some action which visible to you. 您没有执行任何操作,因此添加一些对您可见的操作。 Change it to.
更改为。
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");
}
document.getElementById("troll").innerHTML = trackToUse ;
add this to see the selected option randomly. Here is repaired code for your assistance 这是经过修复的代码,可为您提供帮助
<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>
If you'd like to change the value of your element, you'll need to do something with it after you've retrieved it. 如果要更改元素的值,则在检索元素后需要对其进行一些处理。
For example: 例如:
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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.