2016-07-04 25 views
-3

[解決]HTML/JavaScript的腳本添加到我的網頁

我試圖讓這個當我點擊cuboidCalc 它取代outputOne與在cuboidCalc函數的代碼

<!DOCTYPE html> 
<html> 
<body> 
<p>volume only</p> 
<input type="button" onclick="cuboidCalc()"> cuboidCalc <br> 
<input type="button" onclick="cylinderCalc()"> cylinderCalc <br> 
<input type="button" onclick="triPrismCalc()"> triPrismCalc <br> 

<p id="outputOne"></p> 

<p id="outputTwo"></p> 

<script> 
function cuboidCalc() { 
    document.getElementById("outputOne").innerHTML = "hight: <input type="number" id="hight" value="">"; 

} 
function cylinderCalc() { 

} 
function triPrismCalc() { 

} 
</script> 
+2

是的結果是?馬上? –

回答

0

只要改變你的報價,我認爲:

function cuboidCalc() { 
    document.getElementById("outputOne").innerHTML = "hight: <input type='number' id='hight' value=''>"; 

} 

我也改變了輸入到<button>如果讓任何區別

2

問題出在你的雙引號("..."),因爲你用它們來包圍和指定函數內的屬性值。嘗試更換用單引號封閉或內雙引號是這樣的:

function cuboidCalc() { 
    document.getElementById("outputOne").innerHTML = 'hight: <input type="number" id="hight" value="">'; 
} 

編輯:解釋這更好,字符串語句中的雙引號將聲明停止,因此任何事情以後,會被理解爲代碼,因此將沒有任何意義。這就是爲什麼你必須改變引用的原因,所以人們可以理解爲包含整個語句,而其他人可以在裏面使用它來指定屬性值。如果需要更多引用,請使用反斜槓(\)轉義字符,例如。 statement = '<input onClick="foo(\'bar\')">'

這是demo

相關問題