Javascript 输入元素是否有 onload 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3708850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an onload event for input elements?
提问by imdahmd
Is it possible to trigger a Javascript script, when an input element or any other html element is rendered. This script should be triggered from within the html tag, so that we should be able to pass 'this' to the js function.
是否可以在呈现输入元素或任何其他 html 元素时触发 Javascript 脚本。这个脚本应该从 html 标签内触发,这样我们应该能够将“this”传递给 js 函数。
回答by Pekka
No, there is no such event.
不,没有这样的事件。
However, a <script>tag placed directly after the HTML element would have a similar effect: It would be executed directly after the element has been rendered:
但是,<script>直接放置在 HTML 元素之后的标记会产生类似的效果:它会在元素呈现后直接执行:
<input type="text" id="input123" value="Hello World!">
<script>
alert("Input123 is now ready:"+document.getElementById("input123").value);
</script>
In most cases, however, it is best to use the document-wide load(or DOMReady, or jQuery's .ready()) to start any script operations. The DOM will be fully ready then.
但是,在大多数情况下,最好使用整个文档load(或DOMReady,或 jQuery 的.ready())来启动任何脚本操作。DOM 将完全准备好。
回答by ChrisR
A way to simulate such an event is to create a custom data-* atttribute (HTML-5 valid) and use that as a selector. Then in the main javascript code, you can add a selector for anything which has this specific data-XXX attribute and evaluate the javascript code inside.
模拟此类事件的一种方法是创建自定义 data-* 属性(HTML-5 有效)并将其用作选择器。然后在主要的 javascript 代码中,您可以为具有此特定 data-XXX 属性的任何内容添加选择器并评估其中的 javascript 代码。
Example HTML code:
示例 HTML 代码:
<div data-onload="sampleFunction('param1', 2)"></div>
Example Javascript code (using jQuery). It is also possible to use normal DOM to find elements with this attribute.
示例 Javascript 代码(使用 jQuery)。也可以使用普通 DOM 来查找具有此属性的元素。
/* Find any element which has a 'data-onload' function and load that to simulate an onload. */
$('[data-onload]').each(function(){
eval($(this).data('onload'));
});
This works well. I actually use it in production.
这很好用。我实际上在生产中使用它。
回答by Delan Azabani
No. However, any script placed after the markup of the input element, will be run with the input element available because it is parsed before the script. So, if this was all in the body:
不可以。但是,任何放置在 input 元素标记之后的脚本都将使用可用的 input 元素运行,因为它是在脚本之前解析的。所以,如果这一切都在体内:
<input id="a">
<script>document.getElementById('a');</script>
That would all work anyway.
无论如何,这一切都会奏效。
回答by Collin Anderson
I have an input element that gets dynamically added to the document and needs to get initialized.
我有一个输入元素,它被动态添加到文档中并需要初始化。
It's not perfect, but here's a way to initialize the widget the first time the input is used:
这并不完美,但这里有一种在第一次使用输入时初始化小部件的方法:
<input onfocus="initwidget(this)">
回答by Pointy
No, there is not. Just run your code from the document "ready" handler, or use something like jQuery to give you a way to run code at "ready" (which is when the DOM is complete but images may not have loaded).
不,那里没有。只需从文档“就绪”处理程序运行您的代码,或者使用类似 jQuery 的东西为您提供一种在“就绪”(即 DOM 完成但图像可能尚未加载时)运行代码的方法。
回答by halfdan
No this is not possible. You could however use jQuery + $(document).ready(function) to modify any input field you want right after the page has finished loading.
不,这是不可能的。但是,您可以使用 jQuery + $(document).ready(function) 在页面完成加载后立即修改您想要的任何输入字段。

