Is there an onload event for input elements?

后端 未结 6 707
北恋
北恋 2020-12-01 07:15

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

6条回答
  •  隐瞒了意图╮
    2020-12-01 08:12

    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.

    Example HTML code:

    Example Javascript code (using jQuery). It is also possible to use normal DOM to find elements with this attribute.

    /* 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.

提交回复
热议问题