dsff788655567 2015-03-22 23:37
浏览 86
已采纳

无法在Wordpress中的HTML中调用JS文件中的函数

Quick summary: I have a function in a .js file that's loaded thanks to wp_enqueue_scripts. Now when I try to call that function from the HTML in one of my template files, it doesn't seem to work...

I've researched this and I think I'm doing it all right, but obviously I must have missed something and can't quite put my finger on what it is.


.

So, in functions.php, I have the following code:

function script_assets() {
    wp_enqueue_script( 'js-code', get_template_directory_uri() . '/js/js-code.js' );
}
add_action( 'wp_enqueue_scripts', 'script_assets' );

This works as intended, and results in this being added to the head:

<script type='text/javascript' src='http://.../js/js-code.js?ver=4.1.1'></script>

.

Then, in header.php, after wp_head(); I have the following bit of code:

<?php if ( !is_front_page() ) : ?>
    <script type="text/javascript">
    /* <![CDATA[ */
        setTimeout(function(){ deploy($("#rightsec .deployer")); }, 200);
        setTimeout(function(){ switchMobile($("#switcher")); }, 200);
    /* ]]> */
    </script>

<?php endif; ?>

The PHP bit works correctly, the script tags and everything in between go into the HTML whenever I'm not on the front page as intended. It goes after js-code.js is loaded via the line I pasted above. Both deploy() and switchMobile() are functions in js-code.js and I know they work, as I use them with no issue in a different context.

However Chrome throws

Uncaught ReferenceError: deploy is not defined

Uncaught ReferenceError: switchMobile is not defined

and the functions are not executed...

I know the question of 'how to call JS functions from HTML' has been asked countless times, but like I said I've been reading many pages and they all seem to say the same thing, which I've followed to the letter, and it still doesn't work...

Any ideas? Thanks.


New developments:

So, it turns out that, for some weird reason, if I only call the functions without using setTimeout(), the second one* — switchMobile() — works properly... but not the first one. If I reverse them so that deploy() is after switchMobile(), then this time deploy() works. I am utterly confused :s

*for the sake of clarity my initial example only had one function, when in practice I am calling two. I've edited the question to reflect that.


UPDATE

Tried enclosing the function calls in $(document).ready(function(){ .. });, tried using switchMobile(jQuery('#switcher'));, tried not using JQuery at all (with document.getElementById("switcher"))... Nothing works. Adding an alert() right next to the function calls works as intended...

  • 写回答

1条回答 默认 最新

  • dth62818 2015-03-26 23:10
    关注

    So it was a scope issue and I had to enclose the function call like this:

    jQuery(document).ready(function($) {
        deploy($('#rightsec .deployer'));
    });
    

    There might be other ways to do it but it works like that so I'm not changing it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码