2013-10-15 62 views
1

我购买的HTML主题的基于JQuery的代码是指带有简写的JQuery对象,即$JQuery兼容性和WordPress

但是,WordPress使用Jquery对象作为JQuery - 出于兼容性原因。

我想坚持WordPress的做法,并改变主题的代码与WordPress兼容。

有没有办法将该主题的JavaScript代码与JQuery对象一起使用? 为了更具体一些,我会给你一个例子;

考虑下面的JavaScript代码使用一些JQuery的魔法

/* __________________ 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 Height Columns照顾。

/* ________ 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)。

我刚刚复制了上面的一小段文字,以表达我的观点,而不是让你处理她巨大的custom.js。我认为我抓住的部分足以理解这是如何完成的。如果你想“$”总访问“jQuery的”,而无需在代码的其余部分更换

回答

0

,把这个在那里:

JQuery(document).ready(function($) { 
    // Inside of this function, $() will work as an alias for jQuery() 
    // and other libraries also using $ will not be accessible under this shortcut 
}); 
+0

谢谢你的回答。我仍然得到一个'Uncaught TypeError:无法读取chrome开发人员工具中未定义错误的属性'fn'。安慰。错误在这一行。 '/ * __________________平等的高度__________________ */ $ .fn.eqHeights = function(a){var f = {c ....' –

+0

could this http://stackoverflow.com/questions/10807430/jquery-uncaught- typeerror-can not-read-property-fn-unde-undefined-anonymous-fun是一种补救措施,以避免我收到的错误? - 如果是这样的话,这个包装会去哪里? –

+0

我提供的链接有解决方案。我会回答我自己的问题。感谢您的帮助。 –

1

回答这个问题是在下面的计算器网址:

jQuery Uncaught TypeError: Cannot read property 'fn' of undefined (anonymous function)

但把它带回家,这里是我需要做的:

包裹在这样的custom.js主代码;

(function ($) { 

    // all that $ based code goes here... 

}(jQuery)); 

换句话说,

转到custom.js文件(其位于http://bombdiggitydesign.com/crisp-2/Crisp-warm/assets/js/custom.js),并且这添加到顶部

(function ($) { 

和这对底

}(jQuery)); 

并保存该文件。不需要额外的工作。

这是一个整体的解决方案,只有两行代码,你不仅照顾我所指的EqualHeights部分,而且整个She-Bang的其余部分。他们现在都与WordPress兼容。

希望它可以帮助别人。 - 我肯定会这样做。

0

在我的情况下,问题是与最新版本的jQuery不兼容。这主题functions.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版本。