我购买的html主题基于JQuery的代码引用了带有速记的JQuery
对象,即$
。
然而,WordPress使用Jquery
对象作为JQuery
- 出于兼容性原因。
并更改该主题的代码以与WordPress兼容。
有没有办法将该主题的javascript代码转换为与JQuery对象一起使用?更具体地说,我给你举个例子;
考虑以下JavaScript代码,它使用一些JQuery魔法来处理Equal Height Columns
/* __________________ Equal Heights __________________*/
$.fn.eqHeights=function(a){var f={child:false};var a=$.extend(f,a);var d=$(this);if(d.length>0&&!d.data("eqHeights")){$(window).bind("resize.eqHeights",function(){d.eqHeights()});d.data("eqHeights",true)}if(a.child&&a.child.length>0){var c=$(a.child,this)}else{var c=$(this).children()}var g=0;var b=0;var e=[];c.height("auto").each(function(){var h=this.offsetTop;if(g>0&&g!=h){$(e).height(b);b=$(this).height();e=[]}b=Math.max(b,$(this).height());g=this.offsetTop;e.push(this)});$(e).height(b)};
还要考虑与该部分相关的以下 init。
/* ________ equal heights __________________*/
$(document).ready(function () {
$('.equalHeights').eqHeights();
});
现在。。。我该怎么做才能使上面的代码与WordPress兼容?
我知道我必须在上面的某个关键位置插入JQuery
字符串,但不确定确切的位置。
这个法典页面(下面的链接)是一个很好的资源,但我仍然无法弄清楚这是如何完成的。
http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers
http://bombdiggitydesign.com/crisp-2/Crisp-warm/hero-equal-height-columns.html 的实际代码在这里 http://bombdiggitydesign.com/crisp-2/Crisp-warm/assets/js/custom.js(只需搜索EqualHeights)。
我只是复制了上面的一小节来说明我的观点,而不是让你处理她的巨大习俗.js。我认为我抓住的部分足以理解这是如何完成的。
这个问题的答案在下面的stackoverflow url中:
jQuery uncatch TypeError: 无法读取未定义的属性 'fn' (匿名函数)
但要把它带回家,我需要做的是:
将主代码包装在自定义中.js像这样;
(function ( $ ) {
// all that $ based code goes here...
}( jQuery ));
换句话说,
转到自定义.js文件(位于 http://bombdiggitydesign.com/crisp-2/Crisp-warm/assets/js/custom.js ),并将其添加到顶部
(function ( $ ) {
而这个到底部
}( jQuery ));
并保存了文件。不需要额外的工作。
这是一个整体解决方案,只有 2 行代码,您不仅要照顾我所指的 EqualHeights 部分,还要照顾整个 she-bang 的其余部分。它们现在都与WordPress兼容。
希望它能帮助某人。 - 我相信会的。
就我而言,问题是与最新版本的jQuery不兼容。将其添加到主题的功能中.php作为临时解决方法,直到插件修复:
function thethe_fix() {
wp_deregister_script('jquery');
wp_enqueue_script("jquery",
"//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js", array('json2'),
'1.8.3');
}
add_action( 'wp_print_scripts', 'thethe_fix', 20 );
请注意,这会将您的网站修复到 jQuery 的 1.8.3 版,从长远来看,这可能不是一件好事。