Hướng dẫn how to import html in html - cách nhập html trong html


Tìm hiểu làm thế nào để bao gồm các đoạn trích HTML trong HTML.


HTML

Lưu HTML bạn muốn đưa vào tệp .html:

content.html

Google Maps Nút hoạt hình Box Modal Hộp hoạt hình Tiến trình Thanh treo lơ lửng nhấp vào Dropdowns Bảng đáp ứng bảng
Animated Buttons

Modal Boxes

Animations

Progress Bars

Hover Dropdowns

Click Dropdowns

Responsive Tables


Bao gồm HTML

Bao gồm HTML được thực hiện bằng cách sử dụng thuộc tính W3-Include-HTML:w3-include-html attribute:

Thí dụ

w3-include-html="content.html">


Thêm JavaScript

HTML bao gồm được thực hiện bởi JavaScript.

Thí dụ

Thêm JavaScript



HTML bao gồm được thực hiện bởi JavaScript.

hàm bao gồmHtml () {& nbsp; var z, i, elmnt, file, xhttp; & nbsp; / * Loop thông qua một bộ sưu tập tất cả các yếu tố HTML: */& nbsp; z = document.getElsByTagName ("*"); & nbsp; for (i = 0; i

Thí dụ


Thêm JavaScript


W3.js khá tuyệt.

https://www.w3schools.com/lib/w3.js

Và chúng tôi tập trung

w3-include-html

Nhưng hãy xem xét trường hợp dưới đây

- 🧾 popup.html
- 🧾 popup.js
- 🧾 include.js
- 📂 partials 
   - 📂 head
         - 🧾 bootstrap-css.html
         - 🧾 fontawesome-css.html
         - 🧾 all-css.html
   - 🧾 hello-world.html
<!-- popup.html -->
<head>
<script defer type="module" src="popup.js"></script>
<meta data-include-html="partials/head/all-css.html">
</head>

<body>
<div data-include-html="partials/hello-world.html"></div>
</body>
<!-- bootstrap-css.html -->
<link href="https://...//dist/css/bootstrap.min.css" rel="stylesheet" />

<!-- fontawesome-css.html -->
<link rel="stylesheet" href="https://.../font-awesome/5.15.4/css/all.min.css" />
<!-- all-css.html -->
<meta data-include-html="bootstrap-css.html">
<meta data-include-html="fontawesome-css.html">

<!-- 
If you want to use w3.js.include, you should change as below

<meta w3-include-html="partials/head/bootstrap-css.html">
<meta w3-include-html="partials/head/fontawesome-css.html">

Of course, you can add the above in the ``popup.html`` directly.

If you don't want to, then consider using my scripts.
-->
<!-- hello-world.html -->
<h2>Hello World</h2>

Script

// include.js

const INCLUDE_TAG_NAME = `data-include-html`

/**
 * @param {Element} node
 * @param {Function} cb callback
 * */
export async function includeHTML(node, {
  cb = undefined
}) {
  const nodeArray = node === undefined ?
    document.querySelectorAll(`[${INCLUDE_TAG_NAME}]`) :
    node.querySelectorAll(`[${INCLUDE_TAG_NAME}]`)

  if (nodeArray === null) {
    return
  }

  for (const node of nodeArray) {
    const filePath = node.getAttribute(`${INCLUDE_TAG_NAME}`)
    if (filePath === undefined) {
      return
    }

    await new Promise(resolve => {
      fetch(filePath
      ).then(async response => {
          const text = await response.text()
          if (!response.ok) {
            throw Error(`${response.statusText} (${response.status}) | ${text} `)
          }
          node.innerHTML = text
          const rootPath = filePath.split("/").slice(0, -1)
          node.querySelectorAll(`[${INCLUDE_TAG_NAME}]`).forEach(elem=>{
            const relativePath = elem.getAttribute(`${INCLUDE_TAG_NAME}`) // not support ".."
            if(relativePath.startsWith('/')) { // begin with site root.
              return
            }
            elem.setAttribute(`${INCLUDE_TAG_NAME}`, [...rootPath, relativePath].join("/"))
          })
          node.removeAttribute(`${INCLUDE_TAG_NAME}`)
          await includeHTML(node, {cb})
          node.replaceWith(...node.childNodes) // https://stackoverflow.com/a/45657273/9935654
          resolve()
        }
      ).catch(err => {
        node.innerHTML = `${err.message}`
        resolve()
      })
    })
  }

  if (cb) {
    cb()
  }
}
// popup.js

import * as include from "include.js"

window.onload = async () => {
  await include.includeHTML(undefined, {})
  // ...
}

đầu ra

<!-- popup.html -->

<head>

<link href="https://...//dist/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://.../font-awesome/5.15.4/css/all.min.css" />
</head>

<body>
<h2>Hello World</h2>
</body>

Làm cách nào để nhập HTML vào HTML?

Cách để - bao gồm HTML..
HTML. Lưu HTML bạn muốn đưa vào tệp .html: content.html. ....
Bao gồm HTML. Bao gồm HTML được thực hiện bằng cách sử dụng thuộc tính W3-HTML-HTML: Ví dụ. ....
Thêm JavaScript. HTML bao gồm được thực hiện bởi JavaScript. Thí dụ. ....
Bao gồm nhiều đoạn HTML. Bạn có thể bao gồm bất kỳ số lượng đoạn trích HTML nào:.

Làm cách nào để mở HTML trong HTML?

HTML: Xem html-files..
Bắt đầu trình duyệt của bạn ..
Trong menu "Tệp" Nhấp vào "Mở trang" ....
Trong hộp mới này, nhấp vào "Chọn Tệp" (nếu bạn không thể điền trực tiếp vị trí của tệp).
Sau khi tìm thấy tệp (trong cửa sổ "Trình duyệt tệp"), nhấp vào "OK".

Chúng ta có thể nhập một tệp HTML vào một tệp khác không?

Nhập khẩu HTML, một phần của các thành phần web diễn viên, là một cách để đưa các tài liệu HTML vào các tài liệu HTML khác.Bạn cũng không giới hạn ở đánh dấu.Nhập khẩu cũng có thể bao gồm CSS, JavaScript hoặc bất cứ thứ gì khác.Tệp HTML có thể chứa.. You're not limited to markup either. An import can also include CSS, JavaScript, or anything else an . html file can contain.

Tôi đặt nhập vào HTML ở đâu?

Quy tắc @Import phải ở đầu tài liệu (nhưng sau bất kỳ khai báo @Charet nào).at the top of the document (but after any @charset declaration).