如何将一个html页面基于另一个html页面?

时间:2020-01-05 21:12:58

标签: html

我想对所有其他html页面使用具有通用代码的html页面。我知道如何使用CSS,但是我尝试使用html失败。我正在使用并不断更改的代码出现在所有其他页面上,因此不断浏览每个页面进行更改很烦人。我该怎么办?

3 个答案:

答案 0 :(得分:0)

您可以使用most_common拆分页面内容。例如,如果标头始终相同,则可以将其放置在for i, common in enumerate(scores.most_common(5), 1): print(i, common[0] + " -", common[1]) 中,因此,如果您对其进行修改,则将影响所有页面。 但是我认为这种方法已经过时了

您可以将iframe页转换为iframe,然后将静态代码拆分到另一个html文件中,以便可以在所有页面中php静态内容。 / p>

答案 1 :(得分:0)

您可以使用这种普通的html + js替代方法:https://www.w3schools.com/howto/howto_html_include.asp

,例如:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_include_2

请参见示例:

    <!DOCTYPE html>
<html>
<script>
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;
    }
  }
};
</script>
<body>

<div w3-include-html="h1.html"></div> 
<div w3-include-html="content.html"></div> 

<script>
includeHTML();
</script>

</body>
</html>

答案 2 :(得分:0)

我假设您才刚刚开始。因此,以下是从最容易实现的顺序(不一定是技术上最好的顺序)的三个选项的概括:

  1. 使用iframes。这些允许您将一个HTML页面加载到另一个HTML页面,因此您可以拥有一个包含通用内容的外部页面,而不断变化的内容将进入iframe。

  2. 使用JavaScript,您可以拥有一个基本页面,然后使用JavaScript加载其他内容。一种非常简单的方法是使用具有load()函数的jQuery库,该函数可以满足您的需要。

  3. 在服务器上动态生成HTML。有很多不同的方法。最简单的是PHP。

相关问题