我正在使用WordPress,其中我使用自定义脚本。 实际上在具有特定模板的WP页面上,我想调用自定义脚本。
及其给出错误a.indexOf不是函数。
我的自定义javascript文件为cus.js
以下是其代码
$(window).on('load', function(){
alert('hsdsdsi');
$( ".disabled" ).each(function(index, element){
alert('hsdsdsiasdasdfasfaf');
alert(element.closest("li").attr('class'));
});
});
在functions.php中
我使用以下代码,因为我希望脚本只在加载了包含特定模板的页面时才能运行。
add_filter( 'template_include', 'my_load_script_for_template', 1000 );
function my_load_script_for_template( $template ){
if(is_page_template('template-topcharts.php'))
wp_enqueue_script(get_template_directory_uri() . '/js/cus.js');
return $template;
}
在另一个函数rehub_framework_register_scripts()
中,所有脚本都已注册,我已添加以下代码
function rehub_framework_register_scripts() {
wp_register_script( 'cus', get_template_directory_uri() . '/js/cus.js', array('jquery', 'rehub'), '1.0.0', true );
}
但错误来自其他文件custom.js
答案 0 :(得分:0)
此错误通常是由不兼容的jQuery版本引起的。
更改为
$(document).ready(function(){
alert('hsdsdsi');
$( ".disabled" ).each(function(index, element){
alert('hsdsdsiasdasdfasfaf');
alert(element.closest("li").attr('class'));
});
});
答案 1 :(得分:0)
试试这个。,
function rehub_framework_register_scripts() {
wp_register_script( 'cus', get_template_directory_uri() . '/js/cus.js', array('jquery'), '1.0.0');
}