gpt4 book ai didi

javascript - 表单元素中的 submit()

转载 作者:行者123 更新时间:2023-11-29 20:20:35 25 4
gpt4 key购买 nike

我注意到您可以在表单元素的事件处理程序中调用 submit()。例如,

<input id="myInput" type="button" onclick="submit()" value="Test" />

单击上面代码生成的按钮将提交表单。有趣的是,我不能在事件处理程序之外调用 submit()。没有为输入元素定义的 submit 成员。(document.getElementById("myInput").submitundefined。)所以,在哪里此函数是否已定义,我在哪里可以找到对此函数的引用?

最佳答案

描述here in section 19.1.6 :

Event handlers defined as HTML attributes have a more complex scope chain (...)

The scope chain of an event handler does not stop with the object that defines the handler: it proceeds up the containment hierarchy. For the onclick event handler described earlier, the scope chain begins with the call object of the handler function. Then it proceeds to the Button object, as we've discussed. After that, it continues up the HTML element containment hierarchy and includes, at a minimum, the HTML <form> element that contains the button and the Document object that contains the form. The precise composition of the scope chain has never been standardized and is implementation-dependent. Netscape 6 and Mozilla include all containing objects (even things such as <div> tags), while IE 6 sticks to a more minimal set that includes the target element, plus the containing Form object (if any) and the Document object. Regardless of the browser, the final object in the scope chain is the Window object, as it always is in client-side JavaScript.

所以我想很明显 submit函数是 form 之一.如果事件处理程序是通过 HTML 属性定义的,那么作用域链就是元素本身及其父元素。 但是它不是标准化的,所以不应该依赖这种行为。

关于javascript - 表单元素中的 submit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4120267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com