**题目:** ```html 酱油君1 酱油君2 按钮 ``` ```css #box { position: absolute; top: 20%; left: 40%; width: 300px; padding: 1em; background: #f5f5f5; border: 1px solid #ccc; } ``` 请编写一个方法,实现点击 `#box`(含内部元素)时,浏览器控制台输出 "inner",点击 `#box` 外部空白处时输出 "outer" ---- **参考答案:** ```js const box = document.getElementById('box'); window.addEventListener('click', e => { let elm = e.target; if (elm === box || box.contains(elm)) { // 自身或内部元素 console.log('inner'); } else { console.log('outter'); } }); ``` > [Node.contains 方法](https://developer.mozilla.org/zh-CN/docs/Web/API/Node/contains)
Activity