下面是javascript中的一段,我执行了只显示了 This is the first text the user will see 没有弹出框<html> <head> <title>title</title> </head> <body> <script> sayHi(); </script> <p>This is the first text the user will see</p> <script> function sayHi() { alert("Hi"); } </script> </body> </html> 但是把函数定义放在head里面就有弹出框了 <html> <head> <title>title</title> <script> function sayHi() { alert("Hi"); } </script> </head> <body> <script> sayHi(); </script> <p>This is the first text the user will see</p> </body> </html>
你的 <script> function sayHi() { alert("Hi"); } </script> 方法放在body中,你调用也在body中,调用的时候,这个方法还未被加载,自然没效果! 楼主得搞清楚程序执行的顺序!
不放head照样执行的~~<html> <head> <title>title </title> </head> <body> <p>This is the first text the user will see </p> </body> </html> <script> function sayHi() { alert("Hi"); } </script> <script> sayHi(); </script>
js会影响页面加载,所以一般放到body后更好
你看:</head>
<body onload="fun()">
<div id="oldc">
字符串1
字符串2
字符串3
……
字符串N
</div>
<br />
<div id="newc">
<a href="test.html" target="_blank" onclick="closeWin()">首页</a>
</div>
</body>
<script type="text/javascript">
function fun() {
alert("s");
}
//一般不建议在html标签中调用 事件,可以直接写成window.onload=fun;
</script>
</html>
This is the first text the user will see
没有弹出框<html>
<head>
<title>title</title>
</head>
<body>
<script>
sayHi();
</script>
<p>This is the first text the user will see</p>
<script>
function sayHi()
{
alert("Hi");
}
</script>
</body>
</html>
但是把函数定义放在head里面就有弹出框了
<html>
<head>
<title>title</title>
<script>
function sayHi()
{
alert("Hi");
}
</script>
</head>
<body>
<script>
sayHi();
</script>
<p>This is the first text the user will see</p>
</body>
</html>
<script>
function sayHi()
{
alert("Hi");
}
</script>
方法放在body中,你调用也在body中,调用的时候,这个方法还未被加载,自然没效果!
楼主得搞清楚程序执行的顺序!
<head>
<title>title </title>
</head>
<body>
<p>This is the first text the user will see </p> </body>
</html>
<script>
function sayHi()
{
alert("Hi");
}
</script>
<script>
sayHi();
</script>