我在Wordpress中遵循了一个tutorial on building a jQuery Accordion with custom post types,除了实际的Accordion返回以下错误之外,一切都正常:
Uncaught TypeError: Object [object Object] has no method 'accordion'按照谷歌搜索时发现的建议,我注释掉了header.php中的硬编码jQuery调用,jQuery在functions.php中的加载如下所示:
if ( !is_admin() ) {
wp_deregister_script('jquery');
wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"), false);
wp_enqueue_script('jquery');
}该代码生成以下html:
<div id="#wptuts-accordion">
<h3><a href="">Testimonial 1</a></h3>
<div><p>This is testimonial Text</p></div>
<h3><a href="">Testimonial 2</a></h3>
<div><p>This is testimonial 2</p>
</div>导致错误的jQuery代码如下所示:
jQuery(document).ready(function() {
jQuery("#wptuts-accordion").accordion();
}); 教程代码还包括以下代码,我假设这些代码只是下载jQuery css并注册脚本,但我想知道这是否是问题所在
add_action( 'wp_enqueue_scripts', 'wptuts_enqueue' );
function wptuts_enqueue() {
wp_register_style('wptuts-jquery-ui-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/south-street/jquery-ui.css');
wp_enqueue_style('wptuts-jquery-ui-style');
wp_register_script('wptuts-custom-js', get_template_directory_uri() . '/testimonials/testimonials.js', 'jquery-ui-accordion', '', true);
wp_enqueue_script('wptuts-custom-js');
} jQuery似乎只在页面上加载一次。
有什么办法可以解决这个问题吗?
发布于 2012-12-13 20:35:05
一开始,这可能有点令人困惑。wp_register_script第三个参数必须是依赖项的数组。你有一个字符串。所以我应该是:
wp_register_script(
'wptuts-custom-js',
get_template_directory_uri() . '/testimonials/testimonials.js',
array('jquery', 'jquery-ui-accordion'), // an array of the js files it depend on
'',
true
);常规提示 it您可以按照教程进行操作在问题中提供教程的链接
https://stackoverflow.com/questions/13859276
复制相似问题