我正在使用一个名为showDate()的JavaScript函数,该函数传递了一个参数(称为id)。我正在尝试让函数使用在函数调用中传递给函数的 innerHTML 属性显示日期。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>My bored html</title>
<script type = "text/javascript">
function showDate(id)
{
document.getElementById(id).innerHTML = Date();
}
</script>
</head>
<p id = "Datepara" style = "font-family:comic sans ms;color:orange;text-align:center;font-size:25px;">
Date: 24/9/12 <br /><br /><br/>
</p>
<input type = "button" onclick = "showDate(Datepara)" value = "Display today's date" />
</body>
</html>
更改
showDate(Datepara)
由
showDate('Datepara')
否则,您将传递 html 元素,而不仅仅是 id。
您需要在
元素的 id 字符串两边加上引号:
<!DOCTYPE html>
<html>
<head>
<title>My bored html</title>
<script type = "text/javascript">
function showDate(id)
{
document.getElementById(id).innerHTML = Date();
}
</script>
</head>
<p id = "Datepara" style = "font-family:comic sans ms;color:orange;text-align:center;font-size:25px;">
Date: 24/9/12 <br /><br /><br/>
</p>
<input type = "button" onclick = "showDate('Datepara')" value = "Display today's date" />
</body>
</html>
最好的方法是:
<input type = "button" onclick = "show Date('Datepara')" value = "Display today's date" />
我希望这是有帮助的。