javascript - 排队脚本不适用于 wordpress 中的 jQuery

标签 javascript jquery wordpress

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

我真的需要一些关于这个东西是如何工作的澄清和解释。

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

在 functions.php 文件中:

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 脚本之前,这是在我的页脚中:

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

最佳答案

this post 中有一些答案还有。

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

函数.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 文件中。

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 文件中。

footer.php

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

关于javascript - 排队脚本不适用于 wordpress 中的 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13121550/

相关文章:

JavaScript 问题 *编程新手*

javascript - iPhone Mobile Safari 在从横向旋转到纵向后强制出现顶部栏(避免 "minimal ui")

javascript - 转义 HTML 实体并动态呈现 URL

javascript - SlideToggle 按顺序切换多个 div

javascript - 使用额外的 props 将 React 组件分配给变量

javascript - 在 PHP 的 URL 中使用哈希后的值

javascript - jQuery UI - 放置的元素 -> 从哪个 div 中拖动

jquery - 新版本的 jQuery 说我有语法错误,但没有

包含一串数字的 MySQL ORDER BY FIELD

wordpress - 使用 WordPress 凭据登录 Flutter