我正在将我的html文件转换为WordPress主题,我正在使用插件ZClip将副本文本转换为剪贴板。 ZClip插件在我的html演示中运行正常,但是当转换为WordPress时,我得到了这个奇怪的语法错误“未捕获的TypeError:对象[对象对象]的属性'$'不是zclip.js中第288行的函数文件
$(this.domElement).data('zclipId', 'zclip-' + this.movieId);
我认为这是变量$不确定的东西。我读了一些关于jQuery可能在WP中相互冲突的内容,所以我将main.js文件更改为
jQuery(document).ready(function($){
...
$("button").zclip({
path:'js/ZeroClipboard.swf',
copy: function() { return $(this).attr("data-coupon"); }
});
});
的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并且我使用的是新版本而且因为这行而无法加载
wp_enqueue_script( 'jquery', 'http://code.jquery.com/jquery-1.10.1.min.js' );
也许'jquery'是为WP的本地jquery安装保留的 我将其更改为并且我的网站开始工作,但根据此处的用户不建议这样做。
wp_enqueue_script( 'jq', 'http://code.jquery.com/jquery-1.10.1.min.js' );
答案 0 :(得分:15)
默认情况下,WordPress的jQuery在无冲突模式下运行。在代码中将$
替换为jQuery
,它应该有效。