当我将它转换为Wordpress时,我的JS文件不起作用

时间:2018-03-02 11:53:01

标签: javascript wordpress

我试图将我的HTML页面转换为Wordpress,但Javascript文件未正确加载以允许幻灯片显示功能。

我在控制台Uncaught ReferenceError: plusIndex is not defined at HTMLElement.onclick ((index):143)

中收到此错误

这是JS文件中唯一的代码:

var index = 1;

function plusIndex(n){
  index = index + 1;
  showImage(index);
}

showImage(1);

function showImage(n) {
  var i;
  var x = document.getElementsByClassName("slides");

  if(n > x.length) {index = 1;}
  if(n < 1) {index = x.length;}

  for(i=0; i< x.length; i++) {
    x[i].style.display ="none";
  }
  x[index-1].style.display ="block"
}

这是与之相关的HTML:

<i onclick="plusIndex(-1)" id="head-btn1" class="fa fa-chevron-left head-btn"></i>
<i onclick="plusIndex(1)" id="head-btn2" class="head-btn fa fa-chevron-right"></i>

我已将代码添加到functions.php文件中:

function add_theme_scripts() {
   wp_enqueue_script( 'script', get_template_directory_uri() . '/js/index.js', array(), '1.1', true);
}

我还尝试在wp_register_script( 'script', get_template_directory_uri() . '/js/index.js' );函数上方添加wp_enqueue_script();,但没有运气。

这一切在HTML页面上都可以正常工作

0 个答案:

没有答案