2017-04-19 59 views
1

我使用的是使用自定义脚本的WordPress。 其实在具有特定模板的WP页面上,我想调用一个自定义脚本。jquery给出错误a.indexOf不是函数

及其给出的错误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

enter image description here

+0

'警报;' - 'element'是。(element.closest( “李”)ATTR( '类')。) dom对象,它没有'nearest()'方法 – billyonecan

+0

那么我应该使用$(this).closest(“li”)。attr('class')? – smarttechy

+0

是啊,或'$(元素).closest(...' – billyonecan

回答

0

这即将到来错误通常是由不兼容的jQuery版本引起的。

变化

$(document).ready(function(){ 
    alert('hsdsdsi'); 
     $(".disabled").each(function(index, element){ 
      alert('hsdsdsiasdasdfasfaf'); 


     alert(element.closest("li").attr('class')); 
     }); 
    }); 
+0

我已经看到这个答案,但我没有再次添加jQuery。 – smarttechy

0

试试这个,

function rehub_framework_register_scripts() { 

    wp_register_script('cus', get_template_directory_uri() . '/js/cus.js', array('jquery'), '1.0.0'); 
} 
相关问题