2012-10-01 32 views
1

我正在使用名为showDate()的JavaScript函数传递参数(称为id)。我试图让函数显示与传递给函数调用中的函数的innerHTML属性的日期。JavaScript将不会使用HTML DOM显示日期

这里是我的代码:

<!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> 

回答

2

变化

showDate(Datepara) 

通过

showDate('Datepara') 

否则你传递的HTML元素,而不仅仅是ID。

+0

该死的!我知道这是非常微不足道的东西xD – imulsion

2

你需要围绕元件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> 
+0

你们俩都是对的......我非常恼火,希望有一个html ide – imulsion

+0

Firebug和Chrome的“Inspect Element”功能帮助我很多。 –

+0

有2使用Internet Explorer> :(我在家里有铬 – imulsion

1

最好的方式做到这一点是:

<input type = "button" onclick = "show Date('Datepara')" value = "Display today's date" /> 

我希望这是有帮助的。