document api 실습

2022. 1. 30. 12:18ksmart_html_css_js/HTML실습

728x90
1.
버튼을 클릭 시 해당 버튼의 행이 제거 될 수 있도록 하시오.
<table border="1">
<tbody>
	<tr>
		<td>제목</td>
		<td><button type="button" class="tr-remove">제거</button></td>
	</tr>
	<tr>
		<td>제목</td>
		<td><button type="button" class="tr-remove">제거</button></td>
	</tr>
</tbody>
</table>

 

1. 답
<script type="text/javascript">
var btn=document.querySelectorAll('.tr-remove');
for(var i=0;i<btn.length;i++){
	btn[i].onclick=function(){
		this.parentElement.parentElement.remove();
	}
}
</script>
728x90