简体   繁体   English

jQuery在WordPress中无法正常工作

[英]jQuery does not work properly in WordPress

Basically, I need my posts to animate when the user is hovering over them. 基本上,当用户将鼠标悬停在帖子上时,我需要为它们设置动画。 Here is my code 这是我的代码

It works here, but does not work on my Wordpress page. 它在这里有效,但在我的Wordpress页面上不起作用。 I included it using 我使用了它

<script type="javascript"><?php include('js/ertikis.js');?></script>

I tried including this in the footer file and in the index file, neither works. 我尝试将其包含在页脚文件和索引文件中,但均无效。 The script shows up on the page when inspecting it, but doesn't work. 检查该脚本时,该脚本会显示在页面上,但不起作用。

As far as I know, type of javascript is not valid on <script> . 据我所知, javascript type<script>上无效。

Can you try: <script type="text/javascript"><?php include('js/ertikis.js');?></script> 您可以尝试: <script type="text/javascript"><?php include('js/ertikis.js');?></script>

or if HTML5 you can drop type altogether 或者,如果HTML5可以滴type

<script><?php include('js/ertikis.js');?></script>

The standard convention for including scripts in Wordpress is to use wp_enqueue_script() , flagging jQuery as a dependency. 在Wordpress中包括脚本的标准约定是使用wp_enqueue_script() ,将jQuery标记为依赖项。

Typically this involves including your plugin and other javascript in your Wordpress theme directory, and referencing it as such: 通常,这涉及在您的Wordpress主题目录中包括您的插件和其他JavaScript,并按如下方式引用它:

<?php

// functions.php, in your theme directory

function my_assets() {

    wp_enqueue_script( 'ertikis', get_stylesheet_directory_uri() . '/ertikis.js', array( 'jquery' ) );
    wp_enqueue_script( 'application', get_stylesheet_directory_uri() . '/application.js', array( 'ertikis', 'jquery' ), '1.0', true );
}

add_action( 'wp_enqueue_scripts', 'my_assets' );

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

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