簡體   English   中英

HTML 中引用 DOM 元素的標簽是什么?

[英]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 的更多信息。

element.parentElement.remove(element)中的元素是td4 您應該添加console以輕松打印標簽。

    function remove(element){
       console.log(element);
       element.parentElement.remove(element) **line8**
    }

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM