为什么onclick事件既不适用于JavaScript也不适用于Thymeleaf?

时间:2018-06-13 06:20:54

标签: javascript spring spring-boot thymeleaf

我尝试按下按钮在表格中添加新的动态行。我的项目构建是spring boot,我把HTML放在模板中。我尝试使用" onclick"函数跳转到javascript,然而,它也没有工作" th:onclick"或者" onclick"。

<td><button onclick="add(this)">Add</button></td>
<td><button th:onclick="'javascript:add(this)'">Add</button></td>

这两种方法中的任何一种都注意到我的错误&#34;未捕获的ReferenceError:未定义添加&#34;

以下代码是我的js,我想我定义了函数add()。为什么它不起作用?

function add(object) {
var tr = object.parentNode.parentNode;
var row = tr.rowIndex;
var tab = document.getElementById("tableA");
tab.style.cssText="width:80%; height: 100%; table-layout:fixed";
var objRow1 = tab.insertRow(row++);

var objCell;
var objCell1;
var objCell2;
var objCell3;
var objCell4;
var objCell5;
var objCell6;
var objCell7;
objCell = objRow1.insertCell(0);
objCell.innerHTML='<input type="text" id="SystemName" name="SystemName" value=""/>';
objCell.style.cssText="width:auto; height:auto; word-wrap: break-word;white-space: nowrap;"
objCell1 = objRow1.insertCell(1);
objCell1.innerHTML='<input type="text" name="IP" id="IP" value=""/>';
objCell1.style.cssText="width:auto; height:auto; word-wrap: break-word;white-space: nowrap;"
objCell2 = objRow1.insertCell(2);
objCell2.innerHTML='<input type="text" name="SystemLevel" id="SystemLevel" value=""/>';
objCell2.style.cssText="width:auto; height:auto; word-wrap: break-word;white-space: nowrap;"
objCell3 = objRow1.insertCell(3);
objCell3.innerHTML='<input type="text" name="Ownera" id="Ownera" value=""/>';
objCell3.style.cssText="width:auto; height:auto; word-wrap: break-word;white-space: nowrap;"
objCell4 = objRow1.insertCell(4);
objCell4.innerHTML='<input type="text" name="Ownerb" id="Ownerb" value=""/>';
objCell4.style.cssText="width:auto; height:auto; word-wrap: break-word;white-space: nowrap;"
objCell5 = objRow1.insertCell(5);
objCell5.innerHTML='<input type="text" name="SystemDesc" id="SystemDesc" value=""/>';
objCell5.style.cssText="width:auto; height:auto; word-wrap: break-word;white-space: nowrap;"
objCell6 = objRow1.insertCell(6);
objCell6.innerHTML='<input type="text" name="SystemDate" id="SystemDate" value=""/>';
objCell6.style.cssText="width:auto; height:auto; word-wrap: break-word;white-space: nowrap;"
objCell7 = objRow1.insertCell(7);
objCell7.innerHTML='<input type="text" name="Recorder" id="Recorder" value=""/>';
objCell7.style.cssText="width:auto; height:auto; word-wrap: break-word;white-space: nowrap;"

}

1 个答案:

答案 0 :(得分:0)

您的html代码可能存在阻止运行您的功能的问题。如果至少你的功能正在运行,你试过吗?