Giter VIP home page Giter VIP logo

Comments (7)

CHENSISISI avatar CHENSISISI commented on July 21, 2024 1

// 点击li弹出第几项,不用es6,就是考察的闭包

        var dom = document.querySelectorAll('#list li');
        dom.forEach(function(dom, index) {

            (function(index) {
                dom.addEventListener('click', function(e) {
                    e.stopPropagation();
                    console.log(index);
                });
            }(index));

        });

from frontend-interview.

YuanwuHu avatar YuanwuHu commented on July 21, 2024 1

事件委托 弹窗显示其为当前列表中的第几项

   var list=document.getElementById('list');
   var aLi=list.getElementsByTagName('li');   
       list.addEventListener('click',function(e){
       var target=e.target || e.srcElement;
       if(target.nodeName.toLowerCase() === 'li'){
             for(var i=0,Len=aLi.length;i<Len;i++){
                        if(aLi[i] === target){
                                 alert(i);
                             }
                    }
           }
})

from frontend-interview.

fulvaz avatar fulvaz commented on July 21, 2024

不要给每个 li 都绑一个 listener

    ul.addEventListener('click', e => {
      e.stopPropagation()
      if (e.target.tagName.toLowerCase() === 'li')
      alert(e.target.classList)
    })

from frontend-interview.

ppphs avatar ppphs commented on July 21, 2024

@YuanwuHu 强迫症的我很想在alert(i);后面加个break; XD

from frontend-interview.

Stevenzwzhai avatar Stevenzwzhai commented on July 21, 2024

@upfain 在第六个后面添加应该是let six = document.querySelectorAll('li')[5]吧

from frontend-interview.

zhangyongjie1997 avatar zhangyongjie1997 commented on July 21, 2024

let ul = document.getElementById('ul');
ul.addEventListener('click',click,false)
function click(e) {
let arr = Array.prototype.slice.call(e.target.parentNode.children)
alert(arr.indexOf(e.target));
}

from frontend-interview.

ZHAISHENKING avatar ZHAISHENKING commented on July 21, 2024

借用前面几位老哥的答案,整理一下:

/** 
 * 1. 为ul添加一个类bar
 * 2. 删除第10个li
 * 3. 在第5个li后边添加一个li,文字内容为insert
 * 4. 点击任意一个li弹窗显示其为当前列表中第几项
 */
let ul = document.getElementById('list');
let lis = document.getElementsByTagName('li');
// 添加类
ul.className += ' bar';

// 删除第10个li
lis[9].parentNode.removeChild(lis[9]);
// 第6个li后插入li
let tag = document.createElement('li');
tag.innerHTML = 'insert'
lis[6].parentNode.insertBefore(tag, lis[6])

// 点击
ul.addEventListener('click', e=>{
    e.stopPropagation();
    if (e.target.tagName.toLowerCase() === 'li'){
        let lis = document.getElementsByTagName('li');
        lis = [].slice.call(lis)
        alert(lis.indexOf(e.target))
    }
})

from frontend-interview.

Related Issues (20)

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.