Establishment and use of HTML subpages
With the development of the Internet and web page technology, more and more websites and applications need to use subpages to organize and management information. HTML subpages can make a website more modular and maintainable while providing a better user experience. This article will explain how to create subpages in HTML and provide specific code examples.
1. Create a subpage
<iframe></iframe>
element to embed the child page. The sample code is as follows: <!DOCTYPE html> <html> <head> <title>父页面</title> </head> <body> <h1>这是父页面</h1> <iframe src="子页面.html"></iframe> </body> </html>
<!DOCTYPE html> <html> <head> <title>子页面</title> </head> <body> <h2>这是子页面</h2> <p>这是子页面的内容。</p> </body> </html>
2. Using subpages
src
attribute of the <iframe>
element specifies the file path of the child page. The browser will automatically load the subpage based on the file path and embed it into the parent page. The sample code is as follows: <!DOCTYPE html> <html> <head> <title>父页面</title> </head> <body> <h1>这是父页面</h1> <iframe src="子页面.html"></iframe> </body> </html>
contentWindow
attribute of the <iframe>
element, we can obtain the window object of the subpage. Then, we can use the window object of the subpage to perform various operations, such as modifying the content of the subpage, calling functions of the subpage, etc. The sample code is as follows: <!DOCTYPE html> <html> <head> <title>父页面</title> <script> function changeContent() { var iframe = document.getElementsByTagName('iframe')[0]; var childWindow = iframe.contentWindow; var childDocument = childWindow.document; var childHeading = childDocument.getElementsByTagName('h2')[0]; childHeading.innerText = '子页面内容已修改'; } </script> </head> <body> <h1>这是父页面</h1> <iframe src="子页面.html"></iframe> <button onclick="changeContent()">修改子页面内容</button> </body> </html>
The above code defines a JavaScript function named changeContent()
, which will obtain the <h2># in the sub-page. ## element and modify its text content. Then, add a button to the parent page and bind the function through the </h2>
onclick event. When the button is clicked, the parent page will call the
changeContent() function to modify the content of the child page.
<iframe> elements, we can load and display child pages within the parent page. Through JavaScript, we can operate and control child pages in the parent page. I hope the code examples provided in this article will help you understand and use HTML subpages.
The above is the detailed content of How to create html subpage. For more information, please follow other related articles on the PHP Chinese website!