2012-07-10 103 views
0

我從JS開始,一直在遇到我正在編寫的特定函數的問題。目的是在按下按鈕後更改文本。 「無」文本應該變成「你說你好,我說嘿?」隨着嘿?作爲文件已經在文件上。JS函數調用

這是代碼,任何指針都會受到歡迎!

<html> 
<head> 
<title>Javascript test </title> 
</head> 

<body> 

<p id="hey">Hey?</p> 
<p id ="nothing">Nothing</p> 
<INPUT type =button VALUE="Button?" OnClick=(take())> 

<script> 
function take(){ 
var hey=document.getElementById("hey"); 
var nothing =document.getElementById("one"); 

nothing.appendChild(document.createTextNode("You say hi, I say " + hey)); 
} 
</script> 
</body> 
</html> 

好的,謝謝你們所有的幫助。沒有人特別是100%正確,但我嘗試了所有建議的組合,並獲得了約99%的方式去我想去的地方。我的代碼現在看起來像這樣。我試圖做的最後一項更改不是追加新的文本字符串,而是使用當前追加的字符串替換舊的字符串。

<html> 
<head> 
<title>Javascript test </title> 
</head> 

<body> 

<p id="hey">Hey?</p> 
<p id ="nothing">Nothing</p> 
<input type ="button" value="Button?" OnClick="take();"/> 

<script> 
function take(){ 
var hey=document.getElementById("hey"); 
var nothing =document.getElementById("nothing"); 

nothing.appendChild(document.createTextNode("You say hi, I say " + hey.innerHTML)); 

} 

</script> 
</body> 
</html> 

我欣賞的幫助,謝謝堆棧溢出社區!

+0

所以,你要添加「你說嗨,我說嘿」後一無所獲?你爲什麼得到「嘿」元素? – bokonic 2012-07-10 14:45:26

+1

你可能想看看jQuery。用它做這樣的事情更容易。 – woz 2012-07-10 14:48:10

+0

@woz我不認爲這值得從jQuery的開銷,但這只是我 – jbabey 2012-07-10 14:48:39

回答

2

你的代碼有幾個問題。

首先,您應該在腳本標記上指定type="text/javascript"

其次,你的投入應該是這樣的:

<input id="button" type="button" value="Button?" /> 

第三,你應該綁定在腳本標籤事件處理程序(見SoC):

document.getElementById('button').onclick = take; 

四,您的函數查找一個id爲one的元素,它不存在。你的意思是id nothing?第五,假設您使用正確的ID來獲取nothing元素,則在設置該值時需要使用其他p中的值。

var take = function() { 
    var hey = document.getElementById('hey'); 
    var nothing = document.getElementById('nothing'); 

    nothing.innerHTML= 'You say hi, I say ' + hey.innerHTML; 
}; 

看到這裏工作的例子:http://jsfiddle.net/M5UWn/

作爲一個方面說明,我發現jQFundamentals一個很好的學習網站都jQuery和本地JavaScript。

+0

+1比我的回答:) – 2012-07-10 14:58:42

+0

我發現教程類似於HTML和CSS的JQFundamentals,但沒有找到一個JS。謝謝一堆! – 2012-07-10 15:10:46

0

您輸入改成這樣

<input type=button value="Button?" onclick="take();" /> 
0
<html> 
<head> 
<title>Javascript test </title> 
</head> 

<body> 

<p id="hey">Hey?</p> 
<p id ="nothing">Nothing</p> 
<input type="button" value="Button?" onClick="take()"/> 

<script> 
function take(){ 
var hey=document.getElementById("hey"); 
var nothing =document.getElementById("nothing"); 

nothing.appendChild(document.createTextNode("You say hi, I say "+hey.value)); 
} 
</script> 
</body> 
</html> 

我已經改變了
輸入標籤的
加引號輸入的類型和的OnClick結束
你在做document.getElementById("one")並沒有與元素id one。所以改爲nothing

+0

請解釋您所做的修復,以便人們無需進行字符串比較即可找到您所做的修改。 – jbabey 2012-07-10 14:47:58

+0

這仍然是無效的HTML,沒有關閉輸入標籤加上它真的應該是小寫'輸入' – 2012-07-10 14:48:34

+0

完成..我已經提到所有的變化我做了 – 2012-07-10 14:51:52

0

這裏有幾個指針:

更改您的按鈕輸入HTML看起來像這樣:

<INPUT type="button" VALUE="Button?" OnClick="take()"> 

請注意,所有的屬性都是在雙引號。另外,函數調用的附加括號被刪除。

當你得到'沒有'元素時,你使用了錯誤的ID。它應該是這樣的:

var nothing = document.getElementById("nothing"); 

要更改文本,它更簡單,只需使用此代碼:

nothing.innerText = "You say hi, I say " + hey.innerText;