Heyo!我正在使用getElementById()将favicon的href修改为与图片关联的随机数。但是,每次执行此操作时,我尝试将其分配给的变量为null我做错了什么?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var number = Math.floor((Math.random() * 5) + 1);
var ico = number + ".ico"
document.getElementById("favicon").href = ico <!-- should change the icon -->
</script>
<title>Cool Server People!</title>
<link rel="stylesheet" href="style.css">
<link id="favicon" rel="shortcut icon" href="1.ico" /> <!-- should edit this part -->
</head>
<body>
<div class="header">
<h1>Cool Server People</h1>
</div>
<div class="space">
</div>
<div class="blog">
<a href="test-post.html" class="post">Blog Post</a>
<p>asdfdasdfasdfasdfa</p>
</div>
答案 0 :(得分:1)
<!DOCTYPE html>
<html>
<head>
<title>Cool Server People!</title>
<link rel="stylesheet" href="style.css">
<link id="favicon" rel="shortcut icon" href="1.ico" /> <!-- should edit this part -->
</head>
<body>
<div class="header">
<h1>Cool Server People</h1>
</div>
<div class="space">
</div>
<div class="blog">
<a href="test-post.html" class="post">Blog Post</a>
<p>asdfdasdfasdfasdfa</p>
<script type="text/javascript">
var number = Math.floor((Math.random() * 5) + 1);
var ico = number + ".ico";
document.getElementById("favicon").href = ico; <!-- should change the icon -->
</script>
</div>