首页 > 解决方案 > 命名动态创建的文本区域

问题描述

初学者在这里我的深度!我试图创建文本区域,然后我可以将其发送到数据库。我无法命名文本区域。这是我尝试过的。. .

var node = document.createElement("textarea");
var textnode = document.createTextNode("Comment"); 

node.appendChild(textnode);  

node.setAttribute("id", "commen");
node.setAttribute("name", "comment");

textnode.attr('name', 'com');
textnode.style.width ="100%";

textnode.rows = "400";
textnode.cols ="50";
textnode.id = "myTextArea";
textnode.name = "comments";
textnode.value = "commenters";

node.setAttribute("align", "center");
node.setAttribute("id", "id_you_like");                               

textnode.setAttribute("id", "id_you_like");    

var answerBox = document.getElementById("Answer");
answerBox.style.padding = "0px 0px 0px 0px";

answerBox.setAttribute("align", "center");
answerBox.appendChild(node);

我似乎无法为“Answer”Div 中动态创建的文本区域设置名称。它确实创建了一个 taxtarea,但没有附加名称或 ID。

提前致谢。

标签: javascripttextarea

解决方案


如果您删除文本节点并尝试在节点上设置样式,则会留下 this,如果您使用开发控制台进行检查,它确实有一个带有名称和 id 的 textarea 附加到它:

var node = document.createElement("textarea");
node.setAttribute("id", "commen");
node.setAttribute("name", "comment");
node.setAttribute("align", "center");
node.setAttribute("id", "id_you_like");

var answerBox = document.getElementById("Answer");
answerBox.setAttribute("align", "center");
answerBox.appendChild(node);
<div id="Answer"/>


推荐阅读