在Wordpress中使脚本不适用于jQuery [英] Enque script not working for jQuery in wordpress

查看:68
本文介绍了在Wordpress中使脚本不适用于jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是总是很难让jQuery在wordpress中工作.

I always, always have trouble getting jQuery to work in wordpress.

我真的需要对这件事的工作方式进行澄清和解释.

I really need some clarification and explination on just how this thing works.

这是我的代码,我似乎看不出有什么问题.

Here is my code and I can't seem to see what is wrong with it.

在functions.php文件中:

In the functions.php file:

function my_init() {
if (!is_admin()) {
    // comment out the next two lines to load the local copy of jQuery
    // wp_deregister_script('jquery'); 
    // wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2'); 
    wp_enqueue_script('jquery');
} } add_action('init', 'my_init');

在我调用其他jQuery脚本之前,这在我的页脚中:

And this is in my footer before I call my other jQuery scripts:

<?php wp_enqueue_script("jquery"); ?>

推荐答案

我过去也遇到过这种情况,如果需要的话,我通常甚至不使用wordpress自己的jquery.因为服务器端的google方法更快.下面的代码必须有效.

I've encountered this also in the past, and I usually don't even use the wordpress own jquery if needed. Because the serverside google method is faster. This code below has to work.

functions.php

<?php

function google_jquery() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
    wp_enqueue_script( 'jquery' );
} 

add_action('wp_enqueue_scripts', 'google_jquery');

?>

确保wp_head();在您的 header.php 文件中.

Make sure that wp_head(); is in your header.php file.

header.php

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php wp_title(); ?> <?php bloginfo( 'name' ); ?></title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen" />
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
<?php if ( is_singular() && get_option( 'thread_comments' ) ) wp_enqueue_script( 'comment-reply' ); ?>
<?php wp_head(); ?>
</head>

您无需以这种方式在页脚中调用其他jquery脚本.如果您要入队,请说jquery UI脚本,然后确保您拥有 wp_footer();在您的 footer.php 文件中.

You don't need to call the other jquery scripts in the footer that way. If you're enqueueing lets say jquery UI scripts then make sure you have wp_footer(); in your footer.php file.

footer.php

<?php 
//Footer scripts
wp_footer(); 
?>
</body>
</html>

这篇关于在Wordpress中使脚本不适用于jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆