本帖最后由 不二如是 于 2017-9-9 19:54 编辑
id唯一,所以需要指定两回。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>Insert title here</title>
<script type="text/javascript">
function numb(){
var num1 = parseInt(Math.round((Math.random())*20));
document.getElementById('num1').innerHTML = num1;
var num2 = parseInt(Math.round((Math.random())*20));
document.getElementById('num2').innerHTML = num2;
}
</script>
</head>
<body onload="numb()">
<div>
<span id="num1"></span> + <span id="num2"></span> = <br>
</div>
</body>
</html>
class不唯一,注意语法:<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>Insert title here</title>
<script type="text/javascript">
function numb(){
var num1 = parseInt(Math.round((Math.random())*20));
document.getElementsByClassName("num1")[0].innerHTML = num1;
document.getElementsByClassName("num1")[1].innerHTML = num1;
}
</script>
</head>
<body onload="numb()">
<div>
<span class="num1"></span> + <span class="num1"></span> = <br>
</div>
</body>
</html>
|