如何使用 JS 將按鈕添加到我的 HTML 表格中?該表是使用 JS 使用 XML 檔案資料創建的。
我曾嘗試在創建表格的位置添加按鈕(在 之間<td></td>),但是,沒有任何運氣。
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if(this.readyState == 4 && this.status == 200) {
perDetails(this);
}
};
xmlhttp.open("GET", "../xml/data.xml", true);
xmlhttp.send();
}
function perDetails(xml) {
var i;
var xmlDoc = xml.responseXML;
var table =`<tr><th>Title</th><th>Model</th><th>Price(EUR)</th><th>Delivery Time</th>
<th>In Stock</th><th>Button Column</th></tr>`;
var x = xmlDoc.getElementsByTagName("phones");
console.log(x);
// Start to fetch the data by using TagName
for (i = 0; i < x.length; i ) {
let btn = document.createElement("button");
btn.type = "add";
btn.name = "addBtn";
table = "<tr><td>"
x[i].getElementsByTagName("title")[0]
.childNodes[0].nodeValue "</td><td>"
x[i].getElementsByTagName("model")[0]
.childNodes[0].nodeValue "</td><td>"
x[i].getElementsByTagName("item_price")[0].childNodes[0].nodeValue
"</td><td>"
x[i].getElementsByTagName("delivery_text")[0].childNodes[0].nodeValue
"</td><td>"
x[i].getElementsByTagName("stock")[0].childNodes[0].nodeValue "</td></tr>";
// Print the xml data in table form
document.getElementById("table").innerHTML = table;
}
}
uj5u.com熱心網友回復:
根據您的表頭,您應該在此處添加按鈕:
x[i].getElementsByTagName("stock")[0].childNodes[0].nodeValue "</td>"
"<td><button type='button'>click me</button></td></tr>";
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/388677.html
標籤:javascript
