需求:实现一个九宫格布局,每个格子都有个删除按钮,点击按钮删除当前的宫格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <html> <head> <title></title> </head>
<style type="text/css"> *{padding: 0; margin: 0} ul{display: flex; width: 300px;flex-wrap: wrap; list-style-type: none; margin:50px;} li{ flex:0 0 100px; height: 100px; line-height: 100px; text-align: center; border: 4px solid #ccc; box-sizing: border-box; margin-top: -4px; margin-right: -4px; } li:hover{ border-color: red; z-index: 2 } </style> <body> <ul id="list"> <li>1 <span class="del">X</span></li> <li>2 <span class="del">X</span></li> <li>3 <span class="del">X</span></li> <li>4 <span class="del">X</span></li> <li>5 <span class="del">X</span></li> <li>6 <span class="del">X</span></li> <li>7 <span class="del">X</span></li> <li>8 <span class="del">X</span></li> <li>9 <span class="del">X</span></li> </ul> <script type="text/javascript"> class List{ constructor(dom){ this.ulObj = document.getElementById(dom); this.ulObj.addEventListener('click',(e)=>{ if(e.target.className === 'del') { this.removeItem(e.target) } }) } removeItem(target){ let parent = target.parentNode this.ulObj.removeChild(parent)
} }
window.addEventListener('DOMContentLoaded',function(){ new List('list') })
</script> </body> </html>
|