Nhập khẩu HTML đã được đưa vào một số trình duyệt hiện đại. Vì vậy, nếu bạn muốn thực hiện công nghệ hiện đại thì bạn có thể làm điều đó với chỉ viết một số dòng mã:
<link rel="import" href="import.html" onload="handleLoad(event)" onerror="handleError(event)">
onload
và onerror
là để đăng nhập tình trạng của trang. (Nếu trang nhập đã được tải hay không.)
Tôi đã bao gồm trang nhập của mình (import.html
) vào <template>
thẻ để sao chép nó trong Biến Javascript.
import.html:
<template>
<h1>Hi there!</h1>
<h2>To use html-imports..</h2>
<h3>In Chrome 35 and below(in which you found the flag) you've to enable the flag: <span style="color: brown;">chrome://flags/#enable-html-imports</span></h3>
<h3>In Chrome 36 and Opera 23, it's supported by default. </h3>
</template>
Bạn đã sử dụng Javascript để sử dụng trang nhập khẩu
// Thanks to http://www.html5rocks.com/en/tutorials/webcomponents/imports/
var link = document.querySelector('link[rel="import"]');
// Clone the <template> in the import.
var template = link.import.querySelector('template');
var clone = document.importNode(template.content, true);
document.querySelector('#getting-started-info').appendChild(clone);
function handleLoad(e) {
console.log('Loaded import: ' + e.target.href);
}
function handleError(e) {
console.log('Error loading import: ' + e.target.href);
}
Biến link
được sử dụng để có được các yếu tố nhập khẩu.
Biến số template
được sử dụng để lấy số <template>
từ số import.html
.
Biến số clone
được sử dụng để lấy nội dung của số <template>
trong import.html
.
Sau đó, tôi cố gắng thêm nội dung vào ID của <div>
.
handleLoad
và handleError
được sử dụng để thông báo trạng thái của trang nhập thông qua bảng điều khiển sẽ được hiển thị trong nhiều trình duyệt 'DevTools
'.
Tôi đã viết một article here.
Và Tạo một kho lưu trữ trong Github tại github.com/krman009/html-imports.
html5rocks article.
Tôi ước điều này sẽ giúp bạn.
Có thể bạn cần một máy chủ http. http://stackoverflow.com/a/27222358/1941881 –