Javascript 下拉脚本不会在 Wordpress 页面上执行

问题描述 投票:0回答:1

我一直在尝试创建一些简单的东西来为我的 WordPress 网站开发一个下拉菜单,以便您可以单击一段文本,然后显示带有不同链接的下拉菜单(如您在此处看到的)。我遵循了 W3 学校指导,但我无法使其在我的网站上运行。

我尝试将代码添加到functions.php,但我的网站崩溃了。将其添加到 header.php 时,网站工作正常,但代码不起作用,所以我在这里有点迷失。

我根本不是 Javascript 专家,我似乎找不到问题的根源,所以非常感谢您的建议。

这是我使用的代码:

JAVASCRIPT:

//MENU DROPDOWN DESPLEGABLE AL CLICKAR ENLACES
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
add_action ('wp_head', 'myFunction');

HTML:

<div class="dropdown">
  <p onclick="myFunction()" class="dropbtn">Dropdown</p>
  <div id="myDropdown" class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

CSS:

/* Dropdown Button */
/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
  background-color: #5e7c6d;
  cursor: url(https://imthemoisturizer.com/wp-content/uploads/2020/07/Cursor-2.png), auto;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #ddd; cursor: url(https://imthemoisturizer.com/wp-content/uploads/2020/07/Cursor-2.png), auto;}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}

提前致谢,祝你有美好的一天!

javascript wordpress
1个回答
0
投票

我尝试将代码添加到functions.php,但我的网站崩溃了。

因为你写的代码是Javascript,而functions.php是一个PHP文件,它被评估为PHP。现在,Javascript 不是有效的 PHP 代码,因此您遇到了崩溃。

将其添加到 header.php 时,网站可以正常工作

因为您要么将 Javascript 代码复制到 HTML 中,然后将其显示为纯文本,要么将其添加到其所属的

<script>
标记中,但它还不完美。如果我注释掉最后 JS 行,请参阅下面的代码片段是否有效:

//MENU DROPDOWN DESPLEGABLE AL CLICKAR ENLACES
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
  let myDropdown = document.getElementById("myDropdown");
  if (myDropdown) {
    document.getElementById("myDropdown").classList.toggle("show");
  }
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

//add_action ('wp_head', 'myFunction');
/* Dropdown Button */
/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
  background-color: #5e7c6d;
  cursor: url(https://imthemoisturizer.com/wp-content/uploads/2020/07/Cursor-2.png), auto;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
  position: relative;
  display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

/* Links inside the dropdown */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #ddd; cursor: url(https://imthemoisturizer.com/wp-content/uploads/2020/07/Cursor-2.png), auto;}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
<div class="dropdown">
  <p onclick="myFunction()" class="dropbtn">Dropdown</p>
  <div id="myDropdown" class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

这意味着只要您的 HTML 最终进入页面源代码,如果上面的 Javascript 加载到您的页面中,那么它就会工作。您可以创建一个 myfile.js 文件,在其中放入 Javascript,并通过脚本标签加载该文件,例如

<script src="path/to/myfile.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.