HTML字符串转DOM节点
let myBoxNode = document.getElementById("myBox"); myBoxNode.innerHTML = '<span>朝阳</span>'
DOM节点转HTML字符串
let myBoxNode = document.getElementById("myBox"); console.log(typeof myBoxNode.innerHTML)
完整范例代码
<template> <div id="myBox"> </div> </template> <script> export default { mounted() { // vue中的DOM操作建议都写在$nextTick中 this.$nextTick( () => { let myBoxNode = document.getElementById("myBox"); // HTML字符串转DOM节点 myBoxNode.innerHTML = '<span>朝阳</span>' // DOM节点转HTML字符串 console.log(typeof myBoxNode.innerHTML) } ) } } </script> <style scoped> </style>