题解 | #事件委托#

事件委托

https://www.nowcoder.com/practice/02866b3ce7f8420c8b5d22f483c5fcc0

Make sure you really understand the outcme of the html, "when you click the list item, one dot will become two dots, not the space between marker and dots will become smaller". so the logic will find the clicked item, then change the innerHTML to two dots

<script type="text/javascript">
      /* 2.event logic gose here
        2.1 test the binding
        
        2.2 when you click the item, two dots gather together:one dots is from ul, other one is from html
        2.3 get the clicked list item, then appedn the effect
        2.3 append the css to the dom element

        
        */

      function getEventTarget(e) {
        e = e || window.event;
        return e.target || e.srcElement;
      }
      var ul = document.getElementById("test");

      ul.onclick = function (event) {
        // change the space between the marker and content
        // How do you add CSS with Javascript?
        // https://stackoverflow.com/questions/707565/how-do-you-add-css-with-javascript
        // How to add a class to a given element?
        // https://stackoverflow.com/questions/507138/how-to-add-a-class-to-a-given-element
        // Reducing the gap between a bullet and text in a list item
        // https://stackoverflow.com/questions/2441059/reducing-the-gap-between-a-bullet-and-text-in-a-list-item
        // CSS: Control space between bullet and <li>
          // https://stackoverflow.com/questions/4373046/css-control-space-between-bullet-and-li

        // var styles = `
        // li{
        //   text-indent: -10px;
        // }
        // `;
        // var styleSheet = document.createElement("style");
        // styleSheet.innerText = styles;
        // document.head.appendChild(styleSheet);
        let target = getEventTarget(event);
        // var element = target.getElementsByTagName("li")
        // console.log(element)
        // var target2 = element.classList.add("my-class")
        //  console.log(target2)
        target.innerHTML = ".."
      //  alert( target.innerHTML);
      };
      // code from: http://jsfiddle.net/ArondeParon/2dEFg/5/

    </script>

全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务