[英]What is the tag in HTML that refer to element in DOM?
在第 8 行:- 它有 element.parentElement.remove(element),这里的元素指的是 HTML 中的什么标签?
注意:- 这是小提琴代码,以备不时之需。
Javascript
var td4 = document.createElement('td');
td4.setAttribute("id", "td4");
td4.innerHTML = <button
onclick=remove(this.parentElement)>X</button> *line4
tr.appendChild(td4);
function remove(element){
element.parentElement.remove(element) **line8**
}
HTML
<div class='table'>
<table id="table">
<th>
<tr>
<td>Task</td>
<td>Date</td>
<td>Urgency</td>
<td>Done</td>
</tr>
</th>
<button onclick=clearAll()> Clear All </button>
</table>
</div>
您应该使用模板字符串。
// You'll need to access to the tr element. // Also, I would recommend use const instead of var const tr = document.querySelector('tr'); const td4 = document.createElement('td'); td4.setAttribute("id", "td4"); // You'll need to use the parentElement property twice, because the button will be inserted into the 'td' and you need access to the 'tr' td4.innerHTML = `<button onclick=remove(this.parentElement.parentElement)>X</button>` tr.appendChild(td4); function remove(element){ element.parentElement.remove(element) }
<div class='table'> <table id="table"> <th> <tr> <td>Task</td> <td>Date</td> <td>Urgency</td> <td>Done</td> </tr> </th> <button onclick=clearAll()> Clear All </button> </table> </div>
在此处阅读有关访问 DOM 的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.