onload 事件抛出“预期对象”错误
每当我使用该事件时,我都会收到一个 JavaScript 错误,指出“预期对象”指向包含 onload 事件的行。这是我的网页的简单表示。
<html>
<head>
<script type='text/javascript' src='myScript.js'>
//script containing doSomething()
</script>
</head>
<body onload = "doSomething('stringArg', 0);"> //<--does not execute, throws error, EDITED for proper quotation
//body of site
<script type='text/javascript'>
//test call
doSomething('stringArg', 0); //<--executes just fine, no errors
</script>
</body>
</html>
Whenever I use the event, i get a javascript error saying "Object expected" pointing to the line containing the onload event. Here is a simple representation of my webpage.
<html>
<head>
<script type='text/javascript' src='myScript.js'>
//script containing doSomething()
</script>
</head>
<body onload = "doSomething('stringArg', 0);"> //<--does not execute, throws error, EDITED for proper quotation
//body of site
<script type='text/javascript'>
//test call
doSomething('stringArg', 0); //<--executes just fine, no errors
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对所有内容都使用单引号,因此解析器认为您的属性在
'doSomething('
之后结束使用双引号以避免混淆
希望不会让您感到困惑,但在 javascript 中,引号或多或少是可以互换的,因此您也可以交换它们:
它们不可能全部相同。
You are using single quotes for everything so the parser thinks your attribute ends after
'doSomething('
Use double quotes to avoid confusion
Hopefully not to confuse you, but in javascript, quotes are more or less interchangeable so you could also swap them:
They just can't all be the same.
您有语法错误(因为字符串文字和属性值都使用单引号)。试试这个:
You have a syntax error (because of the single quotes being used for both the string literal and the attribute value). Try this: