当我将它添加到标题部分时,我不清楚它为什么不使用javascript代码,如下所示.
我们可以在主体中放置一个javascript代码,如下所示
<html>
<head>
<title> Simple Test </title>
</head>
<body>
<div id="mydiv"> This is the div content </div>
<script type="text/javascript" >
document.getElementById("mydiv").innerHTML=Date();
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但是当我在标题部分放置相同的JavaScript代码时,它不起作用.
<html>
<head>
<title> Simple Test </title>
<script type="text/javascript" >
document.getElementById("mydiv").innerHTML=Date();
</script>
</head>
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这个问题.我知道我可以在头文件中编写JavaScript函数并在事件中调用它.但我们不能以这种方式使用.如果不能为什么.
小智 5
<html>
<head>
<title> Simple Test </title>
<script type="text/javascript" >
window.onload= function (){document.getElementById("mydiv").innerHTML=Date();}
</script>
</head>
Run Code Online (Sandbox Code Playgroud)
我认为上面的代码可以帮助您解决问题.你可以尝试这个.