切换到jQuery noconflict,如何修复代码? [英] Switching to jQuery noconflict, how do I fix code?

查看:105
本文介绍了切换到jQuery noconflict,如何修复代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是将我的wordpress主题加载jQuery的方式从手动加载google版本切换到将wordpress随附的版本加入队列. WordPress随附的版本中带有"noconflict()",因此$快捷方式不再适用于我的所有jQuery,而是需要在其位置使用"jQuery".

I just switched the way my wordpress theme loads jQuery from manually loading google's version to enqueuing the version that comes with wordpress. The version that comes with wordpress has "noconflict()" in it so the $ shortcut no longer works in all my jQuery and instead I need to use "jQuery" in it's place.

我在页面上有很多.js脚本和很多jquery设置初始化,但是现在都没有用,因为它们都使用速记$.

I have a lot of .js scripts and a lot of jquery setting initializations on pages and now none of it works because it was all using the shorthand $.

我的问题是,假设我需要使用wordpress随附的noconflict版本,那么修复速记代码的最佳方法是什么?

My question is, assuming I NEED to use the noconflict version that comes with wordpress, what is the best way to fix my shorthand code?

我应该在每个脚本的顶部添加这样的行吗?

Should I add a line like this to the top of each script?

jQuery(document).ready(function($) {

还是应该查找文件"全部$并替换为jQuery?

or should I "Find in files" all $ and replace with jQuery?

推荐答案

Wordpress建议:很好的阅读 http://api.jquery.com/jQuery.noConflict/

代码

(function($) {
    // $() will work as an alias for jQuery() inside of this function
})(jQuery);

这篇关于切换到jQuery noconflict,如何修复代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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