WordPress jQuery Uncaught TypeError:对象[object Object]的属性'$'不是函数 [英] WordPress jQuery Uncaught TypeError: Property '$' of object [object Object] is not a function
问题描述
我正在将html文件转换为WordPress主题,并且正在使用插件ZClip将文本复制到剪贴板. ZClip插件在我的HTML演示中可以正常工作,但是在转换为WordPress时,在zclip.js 文件
I'm converting my html files to a WordPress theme and I'm using the plugin ZClip for copy text to clipboard. The ZClip plugin works fine in my html demo, but when converting to WordPress I got this weird syntax error "Uncaught TypeError: Property '$' of object [object Object] is not a function" in line 288 in the zclip.js file which is
$(this.domElement).data('zclipId', 'zclip-' + this.movieId);
我认为不确定$是变量.我读到一些有关jQuery可能在WP中相互冲突的信息,所以我将main.js文件更改为
I think it is something with the variable $ not sure. I read something about the jQuery might get in conflict with each other in WP so I have changed my main.js file to
jQuery(document).ready(function($){
...
$("button").zclip({
path:'js/ZeroClipboard.swf',
copy: function() { return $(this).attr("data-coupon"); }
});
});
functions.php
functions.php
<?php
function load_styles_and_scripts(){
//load css
wp_enqueue_style( 'main-styles', get_template_directory_uri().'/style.css' );
// load scripts
wp_enqueue_script( 'jquery', 'http://code.jquery.com/jquery-1.10.1.min.js' );
wp_enqueue_script( 'zclip-script', get_template_directory_uri().'/js/zclip.js' );
wp_enqueue_script( 'main-script', get_template_directory_uri().'/js/main.js' );
}
add_action('wp_enqueue_scripts', 'load_styles_and_scripts');
整天都想通了,大声笑. 似乎WP 3.5.2加载了旧版本的jQuery 1.8.3,而我使用的是新版本,由于这一行而无法加载
Finally figured this out after a full day, lol. Seems like the WP 3.5.2 load an older version of jQuery 1.8.3 and I using a new version and it it doesn't load because of this line
wp_enqueue_script( 'jquery', 'http://code.jquery.com/jquery-1.10.1.min.js' );
也许'jquery'是为WP的本地jquery安装保留的 我将其更改为,我的网站开始工作,但是根据此处的用户,不建议这样做.
maybe 'jquery' is reserved for WP's local jquery installation I changed it to and my site starts to work, but according to a user here it is not recommended.
wp_enqueue_script( 'jq', 'http://code.jquery.com/jquery-1.10.1.min.js' );
推荐答案
默认情况下,WordPress'jQuery在无冲突模式下运行.在代码中用jQuery
替换$
,它应该可以工作.
By default WordPress' jQuery runs in no conflict mode. Replace $
with jQuery
in your code and it should work.
http://api.jquery.com/jQuery.noConflict/
这篇关于WordPress jQuery Uncaught TypeError:对象[object Object]的属性'$'不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!