简体   繁体   English

JavaScript 未加载到 WordPress

[英]JavaScript doesn't load in WordPress

I've been working in a wordpress template lately, but i'm having a massive problem versus javascript in here.我最近一直在使用 wordpress 模板,但是与这里的 javascript 相比,我遇到了很大的问题。

I've tried:我试过了:

<?php wp_enqueue_script('tools'); ?>

and

 <script src="/(path)/tools" type="text/javascript"></script>

but the strange thing is that I simply put this:但奇怪的是,我简单地说:

<script type="text/javascript">
alert("hello world");    
</script>

and it works just fine它工作得很好

Edit: I'm doing this in the header file编辑:我在 header 文件中执行此操作

Before you enqueue a script you need to register it with: http://codex.wordpress.org/Function_Reference/wp_register_script在将脚本加入队列之前,您需要将其注册到: http://codex.wordpress.org/Function_Reference/wp_register_script

So you would have所以你会有

wp_register_script("tools", get_bloginfo("stylesheet_directory") . "/myscript.js");
wp_enqueue_script("tools");

also remember to put any JS that should run after the DOM has loaded inside of either还记得将任何应该在 DOM 加载后运行的 JS 放入其中

$(document).ready(function() { //code here });

or或者

window.onload = function() { //code here }

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

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