【发布时间】:2014-12-04 18:53:33
【问题描述】:
我正在尝试从 textarea 元素中获取值并将其附加到 p 元素中。问题是它们的功能不同。尝试使用 return 并在函数外部编写它,但没有任何效果。请帮忙。
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>
【问题讨论】:
-
为什么要在代码中创建两个
textAreas? -
您可以简单地将
submitClicked函数移动到createNode函数内。 -
Hey Bergi 我进行了更改并编辑了帖子(更改了双 textArea 的东西,并将 submitClicked 函数放在 createNote 函数中)。但是当我单击提交按钮时,根本没有任何变化,submitClicked 函数中的任何元素都不会被创建,并且控制台中也没有错误。还有其他建议吗?非常感谢!
-
您可能想要使用
submitButton.onclick = submitClicked;中的变量 将函数分配给它:-) -
我是一个初学者,正在尝试破译你的意思。你的意思是哪个变量,我不认为 submitButton.onclick = submitClicked;.... 还有哪个函数?很抱歉不明白,非常感谢!
标签: javascript html function onclick scope