JavaScript,将id作为参数传递给函数
我有一个Id为JavaScript,将id作为参数传递给函数,javascript,html,Javascript,Html,我有一个Id为的。我已为按钮写入onclick事件。单击按钮时,调用函数showDate。我想将'id'作为参数传递给这个showDate函数,这样我的函数就会知道它应该在哪个id上运行 我试过了 <td><button type="button" onclick='document.getElementById("firstApp").innerHTML=Date()'> 那么它工作得很好 但当我将id作为函数调用传递时 <td><button
的
。我已为按钮写入onclick事件。单击按钮时,调用函数showDate
。我想将'id'作为参数传递给这个showDate函数,这样我的函数就会知道它应该在哪个id上运行
我试过了
<td><button type="button" onclick='document.getElementById("firstApp").innerHTML=Date()'>
那么它工作得很好
但当我将id作为函数调用传递时
<td><button type="button" onclick='showDate("firstApp")'>
那就不行了,有人能告诉我哪里出了问题吗
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<tr>
<td>App 1</td>
<td><button type="button" onclick='showDate("firstApp")' name="app1" >on/off</td>
<td>10</td>
<td id="firstApp"></td>
<td></td>
</tr>
<!-- <button type="button">Update</button> <button type="button">Log Out</button> -->
</table>
</div>
<script>
//write script herei
function showDate(appId)
{
document.getElementById("appId").innerHTML = Date();
}
</script>
</body>
</html>
应用程序1
开/关
10
//在这里写剧本
函数showDate(appId)
{
document.getElementById(“appId”).innerHTML=Date();
}
使用参数时,您不希望在参数周围加引号:
function showDate(appId) {
document.getElementById(appId).innerHTML = Date();
// No quotes here ------^----^
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<table>
<tr>
<td>App 1</td>
<td><button type="button" onclick='showDate("firstApp")' name="app1" >on/off</td>
<td>10</td>
<td id="firstApp"></td>
<td></td>
</tr>
<!-- <button type="button">Update</button> <button type="button">Log Out</button> -->
</table>
</div>
<script>
//write script herei
function showDate(appId)
{
document.getElementById(appId).innerHTML = Date();
}
</script>
</body>
</html>
document.getElementById(appId).innerHTML = Date();