<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<p>{{ name }}-{{ describe }}
<div>高启盛{{info.describe}}</div>
</p>
<p>{{ describe }}</p>
<p>{{ info.describe }}</p>
<p>div</p>
</div>
</body>
<script>
const objData = {
name: '高启强',
describe: '黑帮大佬',
info: {
describe: '喜欢吃鱼'
}
}
const app = document.getElementById('app')
const fragment = document.createDocumentFragment()
while (child = app.firstChild) {
fragment.appendChild(child)
}
const regex = /\{\{\s*(\S+)\s*\}\}/
function parseStr(vnode) {
if (vnode.nodeType == 3) {
const text = vnode.textContent
const regRes = text.split(regex)
if (regRes) {
const newContent = regRes.map((i)=> i.split('.').reduce((data, key) => data[key] || key, objData)).join('')
vnode.textContent = newContent
}
}
vnode.childNodes.forEach(element => parseStr(element));
}
parseStr(fragment)
app.appendChild(fragment)
</script>
</html>