列表

详情


FED52. 阻止冒泡事件

描述

请补全JavaScript函数,要求在点击li元素的时候不触发ul的事件。
注意:需要自行获取li元素。

原站题解

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

{"css":"","js":"","html":"<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n        <ul>\n            <li>nowcoder</li>\n        </ul>\n    </body>\n    <script type=\"text/javascript\">\n        // 补全代码\n        var li = document.querySelector('li');\n        li.onclick=function(e){\n            e=window.event|| e;\n            if(e){\n                e.cancelBubble=true;\n            }else{\n                  e.stopPropagation();\n            }\n        }\n        \n    </script>\n</html>","libs":[]}

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

{"css":"","js":"","html":"<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n        <ul>\n            <li>nowcoder</li>\n        </ul>\n    </body>\n    <script type=\"text/javascript\">\n        // 补全代码\n        let li =document.querySelector('li')\n        li.addEventListener('click',function(e){\n            e.stopPropagation()\n        })\n    </script>\n</html>","libs":[]}

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

{"css":"","js":"","html":"<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n        <ul>\n            <li>nowcoder</li>\n        </ul>\n    </body>\n    <script type=\"text/javascript\">\n        // 补全代码\n        var liEl= document.getElementsByTagName('li')[0];\n        liEl.addEventListener(\"click\",function(e){\n            e.stopPropagation();\n        })\n    </script>\n</html>","libs":[]}

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

{"css":"","js":"","html":"<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n        <ul>\n            <li>nowcoder</li>\n        </ul>\n    </body>\n    <script type=\"text/javascript\">\n        // 补全代码\n        var li = document.getElementsByTagName('li')[0]\n        li.onclick = function(e){\n            e.stopPropagation()\n        }\n    </script>\n</html>","libs":[]}

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

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

上一题