如何包含 HTML
学习如何在 HTML 中包含 HTML 片。
HTML
将要包含的 HTML 保存到 .html 文件中:
content.html
<a href="howto_google_maps.asp">Google Maps</a><br> <a href="howto_css_animate_buttons.asp">Animated Buttons</a><br> <a href="howto_css_modals.asp">Modal Boxes</a><br> <a href="howto_js_animate.asp">Animations</a><br> <a href="howto_js_progressbar.asp">Progress Bars</a><br> <a href="howto_css_dropdown.asp">Hover Dropdowns</a><br> <a href="howto_js_dropdown.asp">Click Dropdowns</a><br> <a href="howto_css_table_responsive.asp">Responsive Tables</a><br>
HTML ਸ਼ਾਮਲ ਕਰੋ
ਵਰਤੋਂ w3-include-html
ਵਿਸ਼ੇਸ਼ਤਾ ਵਰਤ ਕੇ HTML ਸ਼ਾਮਲ ਕਰੋ:
ਉਦਾਹਰਣ
ਜੈਵਾਸਕ੍ਰਿਪਟ ਜੋੜੋ
HTML ਸ਼ਾਮਲ ਕਰਨ ਜਿਵੇਂ ਕਿ JavaScript ਰਾਹੀਂ ਕੀਤਾ ਜਾਂਦਾ ਹੈ。
ਉਦਾਹਰਣ
<script> function includeHTML() { var z, i, elmnt, file, xhttp; /* ਸਾਰੇ HTML ਐਲੀਮੈਂਟਾਂ ਦੀ ਕੋਲੈਕਸ਼ਨ ਦੇ ਅੰਦਰ ਲੰਘਣ:*/ z = document.getElementsByTagName("*"); for (i = 0; i < z.length; i++) { elmnt = z[i]; /* ਵਿਸ਼ੇਸ਼ ਅਟਰੀਬਿਊਟ ਵਾਲੇ ਅਣੂਆਮ ਨੂੰ ਲੱਭਣ:*/ file = elmnt.getAttribute("w3-include-html"); if (file) { /* ਅਟਰੀਬਿਊਟ ਦੀ ਕੀਮਤ ਨੂੰ ਫਾਇਲ ਨਾਮ ਵਜੋਂ ਵਰਤ ਕੇ HTTP ਬੇਨਤੀ ਕਰਨ:*/ 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.";} /* ਅਟਰੀਬਿਊਟ ਨੂੰ ਹਟਾਉਣ ਅਤੇ ਫਿਰ ਇਸ ਫੰਕਸ਼ਨ ਨੂੰ ਬੁਲਾਉਣ:*/ elmnt.removeAttribute("w3-include-html"); includeHTML(); } } xhttp.open("GET", file, true); xhttp.send(); /* ਫੰਕਸ਼ਨ ਨੂੰ ਬੰਦ ਕਰਨ:*/ return; } } } </script>
ਪੰਨੇ ਦੇ ਹੇਠਾਂ ਵਿੱਚ ਆਉਣ ਵਾਲੇ ਸ਼ਬਦਾਂ ਵਿੱਚ ਬੁਲਾਉਣ ਵਾਲੇ ਹਨ includeHTML()
:
ਉਦਾਹਰਣ
<script> includeHTML(); </script>
ਬਹੁਤ ਸਾਰੇ HTML ਫਰੈਗਮੈਂਟ ਸ਼ਾਮਲ
ਤੁਸੀਂ ਕਿਸੇ ਵੀ ਸੰਖਿਆ ਵਿੱਚ HTML ਫਰੈਗਮੈਂਟ ਸ਼ਾਮਲ ਕਰ ਸਕਦੇ ਹੋ
ਉਦਾਹਰਣ
<div w3-include-html="h1.html"></div>