gpt4 book ai didi

jquery - 每个人都在我的网站上安装 jQuery 库

转载 作者:行者123 更新时间:2023-12-01 01:25:36 26 4
gpt4 key购买 nike

我正在使用Wordpress,它带有内置的jQuery 库。当我安装插件时,另一个版本就会进入。我学习编写自己的代码,教程要求我安装另一个版本!

在撰写本文时,我可能安装了 4-5 个版本的 jQuery 库!

快疯了!我认为我不需要那么多,我只需要谷歌 CDN 的最新一个以获得最佳性能,所以我将这一行放在我的 php 文件中,但即使在我清除缓存后,它似乎也没有出现在任何地方.

function mytheme_jquery_enqueue(){
if(!is_admin()){
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}
}
add_action('wp_enqueue_scripts', 'mytheme_jquery_enqueue');

如何修复它并删除所有其他不必要的版本?

最佳答案

首先,尝试给你的钩子(Hook)一个较低的优先级,例如:

add_action( 'wp_enqueue_scripts', 'mytheme_jquery_enqueue', 99999 ); 

这样,它将是最新运行的,除非某些插件的优先级为 9999999 ... it's a horse race ...

<小时/>

然后,Enqueuing jQuery in plug-ins

if your code (or the theme's code) is unregistering jQuery and re-registering it from another location (Google), you need to add this somewhere:

jQuery.noConflict(); 

This makes jQuery cooperate with other scripts (i.e. Prototype) that try to define the $ variable globally. WordPress has this line bundled into their version, Google does not.

<小时/>

但是,比自己排队 CDN 更好,建议使用此插件:Use Google Libraries 。根据一位核心开发人员的说法:

Please do not use that method to include the jQuery script from Google. The above doesn't work and will cause conflicts with other scripts, along with other issues. Use the "Use Google Libraries" plugin instead, if you want to use Google hosted versions of the libraries. This plugin does it right, and is updated often. The method above will only work superficially, it will cause problems down the line. – Otto Jul 29 '12 at 15:57

<小时/>

然后,我们遇到的问题是它确实是 not recommended加载其他 jQuery 库并使用与 WordPress 捆绑的库。在这种情况下,您应该使用 remove_action , wp_deregister_scriptwp_dequeue_script以防止所有这些插件或主题加载其版本。

Don’t Dequeue WordPress’ jQuery

As a moderator on the WordPress Stack Exchange, I end up spending a lot of time on the site. I see lots of great questions, lots of not-so-great questions, and several you’ve-got-to-be-kidding-me questions. But the question I see the most often frustrates me:

How do I remove WordPress’ jQuery and use Google’s CDN version instead?

我可以毫不犹豫地说,如果你问这个问题,你 首先没有必要建立网站。

关于jquery - 每个人都在我的网站上安装 jQuery 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14790049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com