我在 html 的 body 标记内渲染了一个 java 脚本函数。在java脚本后面有一个按钮,它调用这个由onclick
属性声明的函数。
<script type="text/javascript">
function f(caller){
console.log('test');
}
</script>
<button onclick="f(this)" type="button">A button</button>
问题是它显示“ReferenceError:f未定义”。我做错了什么?
最佳答案
以下似乎工作得很好:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script type="text/javascript">
function f(caller){
console.log('test');
}
</script>
<body>
<button onclick="f(this)" type="button">A button</button>
</body>
</html>
如果您收到该错误,则可能是来自您提供的代码之外的其他地方。
关于javascript - 在 body 标签内声明函数会导致 "Function not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885597/