2012-05-01 43 views
0

这是我的代码:.innerHTML功能的Javascript

function text(var text) 
{ 
    var Value = document.getElementById("display").innerHTML; 
    var New = Value + text; 
    document.getElementById("display").innerHTML=New; 
} 

什么它应该做的就是从当它被称为一个字符串,然后该字符串添加到使用ID的div元素的结束“显示”。我这样称呼它:text("hello");

该网页的HTML是一个空白的div元素,即使在代码运行后它仍然保持空白。它工作时,我做了document.getElementById("display").innerHTML="hello!";,但不是现在。谢谢!

+2

...所以什么是你的问题?我看不到你需要帮助... – Nightfirecat

+1

你观察到了什么行为? – davesnitty

+0

对不起! :P该网页的HTML是一个空白的div元素,并且即使在代码运行后它仍保持空白。它工作时,我做了document.getElementById("display").innerHTML="hello!";,但不是现在。谢谢! – Andrey

回答

3

不要使用“var”作为函数参数 - 仅在函数括号之间列出它就足够了。所以改变该功能:

function text(text) 
{ 
    var Value = document.getElementById("display").innerHTML; 
    var New = Value + text; 
    document.getElementById("display").innerHTML=New; 
} 

它应该工作。

3

就拿var出来的函数参数:function text(text)...

BTW:不要对你的参数同样的事情,你的功能 - 这是令人困惑的。

0

你可以做这样的:

function text(inputText) 
{ 
    document.getElementById("display").innerHTML = document.getElementById("display").innerHTML +" "+ inputText; 
} 

:)