为什么JavaScript代码无法运行

idea中jsp页面里的JavaScript代码无法运行,alert和console都不行

 

 

 

因为你声明了一个函数function test(){

要执行里面的代码。则调用这个函数,如:console.log(test());

下面三段代码,自己运行以下其中一段,就知道自己错在哪里

function test(){
alert("test");
}
test();
console.log(test());
window.onload = function(){
alert("test");
console.log("test");
}
window.onload = function(){
test();
console.log(test());
}


function test(){
alert("test");
}

 

这边测试没问题,请看动态图演示。

请自行检查浏览器以及你jsp环境是否禁用了JavaScript脚本

 

我的代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp测试JavaScript</title>
</head>
<body>
<table border="1px">
<tr>
	<td>id</td>
	<td>pw</td>
	<td>user</td>
	<td>tel</td>
</tr>
</table>
</body>
<script type="text/javascript">
window.onload = function(){

test();

function test(){
alert("test");
  }
}
</script>
</html>

 

一,
是<script type="text/javascript">
不是<script type="javascript">


二, test()函数只是定义了,但没有调用,需要这样


<script type="text/javascript">
function test() {
    alert("test");
    console.log("test");
}

test();
</script>