admin管理员组文章数量:1425708
Can someone explain me why a function called "action" creates a type error in the following code as soon as the button is surrounded by the form tags. I assume this leads to a strange conflict with the form's action attribute, but i wonder why it happens in this scope ("action" is not defined in any other way):
<html>
<head>
<script type="text/javascript">
function action() {
alert('test');
}
</script>
</head>
<body>
<form>
<input type="button" value="click" onClick="action();">
</form>
</body>
</html>
Can someone explain me why a function called "action" creates a type error in the following code as soon as the button is surrounded by the form tags. I assume this leads to a strange conflict with the form's action attribute, but i wonder why it happens in this scope ("action" is not defined in any other way):
<html>
<head>
<script type="text/javascript">
function action() {
alert('test');
}
</script>
</head>
<body>
<form>
<input type="button" value="click" onClick="action();">
</form>
</body>
</html>
Share
Improve this question
asked Jun 2, 2016 at 11:16
needfulthingneedfulthing
1,09612 silver badges22 bronze badges
3
- 1 I am surprised there is no duplicate for this question – Rayon Commented Jun 2, 2016 at 11:20
- That's the way inline event handlers work. See my answer here: stackoverflow./questions/6941483/onclick-vs-event-handler/… – Felix Kling Commented Jun 2, 2016 at 13:37
- @Rayon: this has been asked before, but I can imagine that it is difficult to name the question or what to look for without knowing what the issue is. I mean, the title of this question is not very indicative of the problem. – Felix Kling Commented Jun 2, 2016 at 13:42
1 Answer
Reset to default 5Inside the form, action
is a string reference to the form action. If you change your onclick to alert(action)
you will get the action of the form (which will be an empty string for your specific form).
In the same manner, form
will be a reference to a form, and method
will contain the form method, if you use them within the form. window.action
will still refer to your function.
本文标签: JavaScript function called quotactionquotStack Overflow
版权声明:本文标题:JavaScript function called "action" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745422439a2657960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论