您可以在JS中使用w3-include-html吗?

时间:2018-07-26 17:23:59

标签: javascript html include

我正在使用.html文件,因为我不太确定如何设置.php include。我一直在使用HTML包含的w3schools方法: https://www.w3schools.com/howto/howto_html_include.asp

但是它不允许我导入JS,也不会将JS应用于我包含的任何HTML。我正在使用以下代码:

	function includeHTML() {
  var z, i, elmnt, file, xhttp;
  /*loop through a collection of all HTML elements:*/
  z = document.getElementsByTagName("*");
  for (i = 0; i < z.length; i++) {
    elmnt = z[i];
    /*search for elements with a certain atrribute:*/
    file = elmnt.getAttribute("w3-include-html");
    if (file) {
      /*make an HTTP request using the attribute value as the file name:*/
      xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4) {
          if (this.status == 200) {elmnt.innerHTML = this.responseText;}
          if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
          /*remove the attribute, and call this function once more:*/
          elmnt.removeAttribute("w3-include-html");
          includeHTML();
        }
      }      
      xhttp.open("GET", file, true);
      xhttp.send();
      /*exit the function:*/
      return;
    }
  }
};
var imported = document.createElement('script');
imported.src = 'js/accordion.js';
document.head.appendChild(imported);

在我的标题中,然后调用

    <script>
includeHTML();
</script>

在index.html中关闭正文标记之前

我如何使手风琴在导航(包含文件)中工作?

www.allisonmwalters.com

这是我手风琴的JS,我现在怀疑是问题所在:

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
    acc[i].addEventListener("click", function() {
        /* Toggle between adding and removing the "active" class,
        to highlight the button that controls the panel */
        this.classList.toggle("active");

        /* Toggle between hiding and showing the active panel */
        var panel = this.nextElementSibling;
        if (panel.style.display === "block") {
      	panel.style.display = "none";
        } else {
            panel.style.display = "block";
        }
    });
}

0 个答案:

没有答案