我試圖從一個textarea元素的值,並將其附加到p元素。問題在於它們具有不同的功能。已嘗試使用返回並在函數外寫入,但沒有任何工作。請幫忙。作用域 - 從局部變量獲取值?
的javascript:
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
var newNote = document.getElementById("newNote");
var createNote = function(){
var noteTitleValue = prompt("Please label the new note:");
var noteContainer = document.createElement("li");
var textArea = document.createElement("textarea");
var noteTitle = document.createElement("label");
var submitButton = document.createElement("button"); submitButton.innerHTML = "Submit";
noteContainer.appendChild(noteTitle);
noteContainer.appendChild(textArea);
noteContainer.appendChild(submitButton);
div1.appendChild(noteContainer);
noteTitle.innerText = (noteTitleValue);
return textArea;
submitButton.onclick = submitClicked;
// submitButton.onclick = div1.removeChild(noteContainer);
var submitClicked = function(){
console.log(textArea.value); // not working.
console.log("test"); // not working either.
var savedNoteContainer = document.createElement("li");
var pArea = document.createElement("p");
savedNoteContainer.appendChild(pArea);
div2.appendChild(savedNoteContainer);
var textValue = (textArea.value);
pArea.innerHTML = textValue;
};
};
newNote.onclick = createNote;
CSS:
#div1 {
width: 200px;
height: 200px;
border: solid 1px #000;
}
#div2 {
width: 200px;
height: 200px;
border: solid 1px #000;
}
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Todo App</title>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8">
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<button id="newNote">Add Note</button>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
爲什麼在代碼中創建兩個'textArea's? – Bergi 2014-12-04 18:56:41
您可以簡單地在'createNode'函數中移動'submitClicked'函數。 – Bergi 2014-12-04 18:58:57
嘿Bergi我做了更改和編輯帖子(改變了雙文本區域的東西,並把submitClicked函數內的createNote函數)。但是,當我單擊提交按鈕時根本沒有任何更改,submitClicked函數中的任何元素都不會創建,並且控制檯中也沒有錯誤。還有其他建議嗎?非常感謝! – 2014-12-04 19:04:53