Uncaught ReferenceError: renderer is not defined at,为什么不对,出不来alert警告框

628 篇文章 6 订阅
<!DOCTYPE html>
<html>
<div id = "vnode.tag">
  <button class="btn" style="width:100px;height:50px">click me</button>
</div>

//<script type="text/javascript"></script>
<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 type="text/javascript"></script>
<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){
    // 把container一层层传递下去
    const el = vnode.el = document.createElement(vnode.tag);
    // props 处理
    if(vnode.props){
        for(const key in vnode.props){
            const value = vnode.props[key];
            // props 有很多类型,这里假设都是 attribute
            el.setAttribute(key,value)
        }
    }
   // 处理 children
   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 type="text/javascript"></script>
<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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值