我很新的HTML和JavaScript。如何使用javascript获取html值?
每当用户使用javascript单击表格行时,我都想获取元素的内容。
test.html
<html>
<head>
<script text="text/javascript">
function dispTblContents() {
var pName = document.getElementById("pName").value;
var pAddress = document.getElementById("pAddress").value;
var pEmail = document.getElementById("pEmail").value;
alert(pName + " " + pAddress + " " + pEmail);
}
</script>
</head>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address </th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr onclick="dispTblContents();" >
<td id="pName">Ricardo Lucero</td>
<td id="pAddress">Mexico City, Mexico</td>
<td id="pEmail">[email protected]</td>
</tr>
</tbody>
</table>
</body>
</html>
每当我点击它显示undefined undefined undefined
行。我知道我的代码是错误的,但我真的不知道如何解决这个问题。有人能帮帮我吗。我对这件事很陌生。提前致谢。
使用* innerText *或* textContent *(根据情况)会更好,因此不会返回标记。 – RobG