日期:2014-05-16  浏览次数:20443 次

下面这段JavaScript代码错在哪里呢?


<html>
<head>
<meta charset="utf-8" />
</head>

<script type="text/javascript">

function fun(key) {
alert(key);
}

</script>

<body onload=test(["hi", "hi2"])>

</body>
</html>

------解决方案--------------------
哪来的test啊
------解决方案--------------------
你都没有定义test函数,肯定报错,应该改为:<body onload='fun(["hi", "hi2"])'>
------解决方案--------------------
告诉我,这里不是CSDN,这里是大家来找茬,对吗?

------解决方案--------------------
引用:
Quote: 引用:

你都没有定义test函数,肯定报错,应该改为:<body onload='fun(["hi", "hi2"])'>





<html>
<head>
<meta charset="utf-8" />
</head>

<script type="text/javascript">

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

</script>

<body onload=test(["hi", "hi2"])>

</body>
</html>


修改完代码,还是不能执行啊
我想要做的是在onclick里调用JS函数,并且在这里向该函数传 JS对象,为何就不行呢?
["hi", "hi2"]不就是JS对象么:(

在标准里面是这么写的:
In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). We recommend using quotation marks even when it is possible to eliminate them.
属性不加引号的情况是属性值只包含字母、数字、连字符、点号、下划线和冒号。
所以onload=test(["hi", "hi2"])应该加上引号,为onload='test(["hi", "hi2"])'
------解决方案--------------------
引用:
Quote: 引用:

你都没有定义test函数,肯定报错,应该改为:<body onload='fun(["hi", "hi2"])'>





<html>
<head>
<meta charset="utf-8" />
</head>

<script type="text/javascript">

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

</script>

<body onload=test(["hi", "hi2"])>

</body>
</html>


修改完代码,还是不能执行啊
我想要做的是在onclick里调用JS函数,并且在这里向该函数传 JS对象,为何就不行呢?
["hi", "hi2"]不就是JS对象么:(

加单引号
<body onload='test(["hi", "hi2"])'>