Javascript 给出错误a.indexOf的Jquery不是函数
我使用的是WordPress,其中我使用的是自定义脚本。 实际上,在具有特定模板的WP页面上,我想调用一个自定义脚本 它的给定错误a.indexOf不是一个函数 我的自定义javascript文件是Javascript 给出错误a.indexOf的Jquery不是函数,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我使用的是WordPress,其中我使用的是自定义脚本。 实际上,在具有特定模板的WP页面上,我想调用一个自定义脚本 它的给定错误a.indexOf不是一个函数 我的自定义javascript文件是cus.js 下面是它的代码 $(window).on('load', function(){ alert('hsdsdsi'); $( ".disabled" ).each(function(index, element){ alert('hsdsd
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\u framework\u register\u 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中出现了错误
此错误通常是由不兼容的jQuery版本引起的 改为
$(document).ready(function(){
alert('hsdsdsi');
$( ".disabled" ).each(function(index, element){
alert('hsdsdsiasdasdfasfaf');
alert(element.closest("li").attr('class'));
});
});
此错误通常是由不兼容的jQuery版本引起的 改为
$(document).ready(function(){
alert('hsdsdsi');
$( ".disabled" ).each(function(index, element){
alert('hsdsdsiasdasdfasfaf');
alert(element.closest("li").attr('class'));
});
});
试试这个
function rehub_framework_register_scripts() {
wp_register_script( 'cus', get_template_directory_uri() . '/js/cus.js', array('jquery'), '1.0.0');
}
试试这个
function rehub_framework_register_scripts() {
wp_register_script( 'cus', get_template_directory_uri() . '/js/cus.js', array('jquery'), '1.0.0');
}
我已经看到这是一个答案,但我不会再添加jquery。我已经看到这是一个答案,但我不会再添加jquery。
alert(element.closest(“li”).attr(“class”)
-element
是一个dom对象,它没有最近的()
方法,那么我应该使用$(this.nexist(“li”).attr('class')?是的,或者$(element.).nexist(…
)但是即使我注释掉这一行,也会发生此错误警报(element.nexist(“li”).attr('class'));
-元素
是一个dom对象,它没有最近的()
方法,那么我应该使用$(this.nestest(“li”).attr('class')?是的,或者$(element.)。最近的(…
),但即使我注释掉这一行,也会发生此错误