这个javascript不是写在<head>里的吗?我觉得好像要在<body>里写onload=才会打开页面时自动加载,不是吗

来源:百度知道 编辑:UC知道 时间:2024/06/14 16:55:19
但是这样写已经自动加载运行了

<html>
<head>
<meta http-equiv="content-type" content="text / ht ml;charset=GB2312"/>
<title>2-1</title>
<script type="text/javascript">
what_to_say="你好";
alert("what_to_say);
</script>
</head>
<body style="overflow:auto;">
</body>
<h t ml>

你这个就是语句不是方法
如果你写成方法得形式就要在onload里面加调用得方法得名称了

如:

<html>
<head>
<meta http-equiv="content-type" content="text / ht ml;charset=GB2312"/>
<title>2-1</title>
<script type="text/javascript">
function doit(){
what_to_say="你好";
alert(what_to_say);
}
</script>
</head>
<body onload="doit()" style="overflow:auto;">
</body>
<html>

顺便说下:alert("what_to_say);应该是alert(what_to_say);
你多了半个引号