列表

详情


FED1. 事件委托

描述

请补全JavaScript代码,要求如下:
1. 给"ul"标签添加点击事件
2. 当点击某"li"标签时,该标签内容拼接"."符号。如:某"li"标签被点击时,该标签内容为".."
注意:
1. 必须使用DOM0级标准事件(onclick)

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 658ms, 内存消耗: 114820KB, 提交时间: 2022-01-17

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t<ul>\n            <li>.</li>\n            <li>.</li>\n            <li>.</li>\n        </ul>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            let ul = document.querySelector('ul');\n            ul.addEventListener('click', (e) => {\n                let event = e || window.event;\n                let target = event.target || event.srcElement;\n                target.innerText = target.innerText + '.';\n            })\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1679ms, 内存消耗: 77808KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t<ul>\n            <li>.</li>\n            <li>.</li>\n            <li>.</li>\n        </ul>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            let ulTag = document.getElementsByTagName('ul')[0]\n            ulTag.addEventListener('click', event => {\n                let ev = event || window.event\n                let target = ev.target || ev.srcElement;\n                if (target.nodeName.toLowerCase() === 'li'){\n                    target.innerHTML += '.'\n                }\n            })\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1687ms, 内存消耗: 77808KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t<ul>\n            <li>.</li>\n            <li>.</li>\n            <li>.</li>\n        </ul>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            const ul = document.querySelector('ul');\n            ul.onclick = function (e) {\n                e.target.innerText += '.';\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1739ms, 内存消耗: 77860KB, 提交时间: 2021-12-08

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t<ul>\n            <li>.</li>\n            <li>.</li>\n            <li>.</li>\n        </ul>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            //获取事件的根节点 \n            let ulTags = document.getElementsByTagName('ul')[0]\n            //对根节点绑定事件  => 回调函数的参数为事件本身 因事件有taget 属性所以可以得知是那个子节点被点击\n            ulTags.addEventListener('click',(event)=>{\n                //  兼容性判断\n                let ev = event || window.event;\n                var target = ev.target || ev.srcElement;\n                //对点击的子节点进行处理\n                if(target.nodeName.toLowerCase() == 'li'){\n                    target.innerHTML+='.';\n                }\n            })\n        </script>\n    </body>\n</html>","libs":["jquery"]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77812KB, 提交时间: 2021-12-13

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t<ul>\n            <li>.</li>\n            <li>.</li>\n            <li>.</li>\n        </ul>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            /*\n            var ul = document.querySelector('ul')\n            ul.addEventListener('click',function(){\n                event.target.innerHTML = \"..\";\n            })\n            */\n            document.querySelector('ul').onclick = function(e){\n                e.target.innerText = '..'\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题