<!DOCTYPE html>
<html>
<div id = "vnode.tag">
<button class="btn" style="width:100px;height:50px">click me</button>
</div>
<script>
const vnode = {
tag: 'div',
props: {
onClick: () =>alert('hello')
},
children: 'click me'
}
function render(vnode, container) {
const el = document.createElement(vnode.tag)
for (const key in vnode.props) {
if(/^on/.test(key)) {
el.addEventListener(
key.substre(2).toLowerCase(),
vnode.props[key]
)
}
}
if (typeof vnode.children === 'string') {
el.appendChild(document.createTextNode(vnode.children))
} else if (Array.isArray(vnode.children)) {
vnode.children.forEach(child => renderer(chind, el))
}
container.appendChild(el)
}
renderer(vnode, document.body)
</script>
</html>
错误的
<!DOCTYPE html>
<html>
<div id = "app">
<button class="btn" style="width:100px;height:50px">click me</button>
</div>
<script>
const vdom = h('div', {class:'red'}, [
h('span', null['hello'])
])
mount(vdom, document.getElementById('app'))
const vnode = {
tag: 'div',
props: {
onClick: () =>alert('hello')
},
children: 'click me'
}
function render(vnode, container) {
const el = document.createElement(vnode.tag)
for (const key in vnode.props) {
if(/^on/.test(key)) {
el.addEventListener(
key.substre(2).toLowerCase(),
vnode.props[key]
)
}
}
if (typeof vnode.children === 'string') {
el.appendChild(document.createTextNode(vnode.children))
} else if (Array.isArray(vnode.children)) {
vnode.children.forEach(child => renderer(chind, el))
}
container.appendChild(el)
}
renderer(vnode, document.body)
</script>
</html>
还是不知道咋整,乱七八糟的
<html>
<style>
.red {color: #f00;font-size:24px;}
</style>
<div id="app">
<button>click me</button></div>
<script>
const vdom = h('div',{class:'red'},[
h('span',null,['hello'])
])
mount(vdom,document.getElementById('app'))
const vnode = {
tag: 'div',
props: {
onClick: () =>alert('hello')
},
children: 'click me'
}
function h(tag,props,children){
return {
tag,
props,
children
}
}
function mount(vnode,container){
const el = vnode.el = document.createElement(vnode.tag);
if(vnode.props){
for(const key in vnode.props){
const value = vnode.props[key];
el.setAttribute(key,value)
}
}
if(vnode.children){
if(typeof vnode.children === 'string'){
el.textContent = vnode.children;
}else{
vnode.children.forEach(child=>{
mount(child,el)
})
}
}
container.appendChild(el)
}
</script>
</html>
<html>
<style>
.red {color: #f00;font-size:24px;}
</style>
<div id="app">
<button>click me</button></div>
<script>
const createNode = (tag, props, children) => ({
tag,
props,
children,
});
createNode('div', { id: 'app' }, ['Hello World']);
const createElement = vnode => {
if (typof vnode === 'string') {
return document.createTextNode(vnode);
}
const el = document.createElement(vnode.tag);
if (vnode.props) {
Object.entries(vnode.props).forEach(([name, value]) => {
el[name] = value;
});
}
if (vnode.children) {
vnode.children.forEach(child => {
el.appendChild(createElement(child));
});
}
return el;
createElement(createNode("div", { id: "app" }, ["Hello World"]));
</script>
</html>
<!DOCTYPE html>
<html>
<div id = "vnode.tag">
<button class="btn" style="width:100px;height:50px">click me</button>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script>
import { h } from 'vue'
export default {
render() {
return h('h1', { onClick: handler })
}
}
const vnode = {
tag: 'div',
props: {
onClick: () =>alert('hello')
},
children: 'click me'
}
function render(vnode, container) {
const el = document.createElement(vnode.tag)
for (const key in vnode.props) {
if(/^on/.test(key)) {
el.addEventListener(
key.substre(2).toLowerCase(),
vnode.props[key]
)
}
}
if (typeof vnode.children === 'string') {
el.appendChild(document.createTextNode(vnode.children))
} else if (Array.isArray(vnode.children)) {
vnode.children.forEach(child => renderer(chind, el))
}
container.appendChild(el)
}
renderer(vnode, document.body)
</script>
</html>
|