Code Monkey home page Code Monkey logo

Comments (3)

boybird avatar boybird commented on July 28, 2024
<body>

    <ul>
        <li class='target'>
            <span>test</span>
       </li>
      </ul>


    <script>
        function getEventTarget(e) {
            e = e || window.event;
            return e.target || e.srcElement;
        }
        document.getElementsByTagName('ul')[0].addEventListener('click', (e)=>{
            let target = getEventTarget(e);
            if(target.classList.contains("target")){
                alert("test");
            }
        })
    </script>
</body>

from logic-training.

erbing avatar erbing commented on July 28, 2024
let appRom = document.getElementById('delegation')
let ulDom = appRom.children[0]

// 绑定方法
const handle = (dom, type, callback) => {
    dom.addEventListener(type,callback)
}

// 获取 target 对象方法 兼容
const getEventTarget = (e) => {
    e = e || window.event
    return e.target || e.srcElement
}

// 动态添加一个 btn 按钮
const createBtn = () => {
    let createBtnDom = document.createElement('button')
    createBtnDom.textContent = '添加'
    createBtnDom.className = 'add-btn'
    handle(createBtnDom, 'click', ()=>{
        let createLiDom = document.createElement('li')
        createLiDom.textContent = 'hello, li~' + Math.random()
        ulDom.appendChild(createLiDom)
    })
    return createBtnDom
}

// 执行 添加一个 btn 按钮 方法
let createBtnHtml = createBtn()
appRom.appendChild(createBtnHtml)

// 获得点击 target 的 innerHtml 方法
handle(appRom,'click',(e)=>{
    let curTarget = getEventTarget(e)
    if (curTarget.children && curTarget.children.length || curTarget.tagName == 'BUTTON') return false 
    return curTarget.innerHTML
})

from logic-training.

Xiphin avatar Xiphin commented on July 28, 2024
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div id="delegation">
    <ul>
        <li class='target'>
            <span>test1</span>
       </li>
    </ul>
</div>
</body>
<script>
let appRom = document.getElementById('delegation')
let ulDom = appRom.children[0]

// 绑定方法
const handle = (dom, type, callback) => {
    dom.addEventListener(type,callback)
}

// 获取 target 对象方法 兼容
const getEventTarget = (e) => {
    e = e || window.event
    return e.target || e.srcElement
}

// 动态添加一个 btn 按钮
const createBtn = () => {
    let createBtnDom = document.createElement('button')
    createBtnDom.textContent = '添加'
    createBtnDom.className = 'add-btn'
    handle(createBtnDom, 'click', ()=>{
        let createLiDom = document.createElement('li')
        createLiDom.textContent = 'hello, li~' + Math.random()
        ulDom.appendChild(createLiDom)
    })
    return createBtnDom
}

// 执行 添加一个 btn 按钮 方法
let createBtnHtml = createBtn()
appRom.appendChild(createBtnHtml)

// 获得点击 target 的 innerHtml 方法
handle(appRom, 'click', (e)=>{
    let curTarget = getEventTarget(e)
    if (curTarget.children && curTarget.children.length || curTarget.tagName == 'BUTTON') return false 
    alert(curTarget.innerHTML)
})
</script>
</html>

from logic-training.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.