简体   繁体   English

为什么我的 onload 在 Wordpress 中不起作用?

[英]Why is my onload not working in Wordpress?

I really need to use a onload and I don't understand why it's not working.我真的需要使用 onload,但我不明白为什么它不起作用。 I made a code snippet to test it out and yet again found that it didn't work.我做了一个代码片段来测试它,但再次发现它不起作用。 Is wordpress doing this? wordpress 是这样做的吗? Or am I making a mistake?还是我犯了一个错误? I found that events such as onclick and onchange are working.我发现诸如 onclick 和 onchange 之类的事件正在工作。

function do_onload_event()
{
    return '<div id="loadevent" onload="do_show_event(event)"></div>' . '<script>
    function do_show_event(event)
    {
        console.log("on load event");
    }
    </script>';
}
add_shortcode('on_load_event','do_onload_event');

IMHO - Calling onload from in-post shortcode could have many issues of timing or executions.恕我直言 - 从 in-post 短代码调用 onload 可能会出现许多时间或执行问题。 Also, I am not sure that it is possible ot hook to onLoad() or onready() more than once so it would be more compatible..另外,我不确定是否可以多次挂接到onLoad()onready()上,所以它会更兼容..

Since we are dealing with wordpress, you could try another approach using jQuery:由于我们正在处理 wordpress,您可以尝试使用 jQuery 的另一种方法:

function add_my_scripts() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'my_init_script', 'MyScriptSource/etc', 'jquery', '1.0' ); // jQuery dependency
}
add_action( 'init', 'add_my_scripts' );

Then add another script that calls your method.然后添加另一个调用您的方法的脚本。

jQuery.noConflict();

jQuery(document).ready(function($) {
    init();
});

You could of course try also for load event您当然也可以尝试加载事件

jQuery(window).load(function($) {
   // your function
});

or..或者..

(function($) {
  // your function
})(jQuery);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM