简体   繁体   English

无法以正确的方式附加孩子

[英]Not able to append child in correct way

I have textarea and I want to append a paragraph tag, before I can get the value of textarea. 我有textarea,我想附加一个段落标签,然后才能获取textarea的值。

I am trying to do it with Javascript create element 我正在尝试使用Javascript create元素完成此操作

var desc = document.createElement("p");
desc.innerHTML = description;
var abc = document.getElementById("textareaCode").appendChild(desc);
alert (abc);
 var comment = document.getElementById('textareaCode').value;
alert (comment);

This is the error message 这是错误消息

[object HTMLParagraphElement] [对象HTMLParagraphElement]

A textarea does not have the value property. Textarea没有value属性。 You can get what's between the tags by using innerHTML : 您可以使用innerHTML获取标签之间的内容:

 var desc = document.createElement("p"); desc.innerHTML = 'blablabla'; var abc = document.getElementById("textareaCode").appendChild(desc); var comment = document.getElementById('textareaCode').innerHTML; alert (comment); 
 <textarea id="textareaCode"></textarea> 

Note as Victor Popescu commented, that even if natively the HTML attribute value doesn't exist for a textarea, the setter exists in javascript so you can dynamically change the content with document.getElementById('textareaCode').value = 'test'; 请注意,正如Victor Popescu所评论的那样,即使本机不存在textarea的HTML属性value ,setter也存在于javascript中,因此您可以使用document.getElementById('textareaCode').value = 'test';来动态更改内容document.getElementById('textareaCode').value = 'test'; (but that won't add the attribute to the HTML, and more surprisingly won't update the DOM content, even if innerHTML will give you the right updated value). (但这不会将属性添加到HTML,更令人惊讶的是,即使innerHTML会为您提供正确的更新值,也不会更新DOM内容)。

About the rest of your problem, you can manipulate directly innerHTML to add text to existing one. 关于其余问题,您可以直接操作innerHTML将文本添加到现有innerHTML中。 But note that HTML tags will be rendered as text ( < becomes &lt; , etc). 但请注意,HTML标签将呈现为文本( <变为&lt;等)。 Either you append an HTML object and it won't show in the textarea, either as text and it won't be treated as HTML: 要么添加一个HTML对象,它就不会在文本区域中显示为文本,也不会被视为HTML:

 var description = 'blablabla'; document.getElementById('textareaCode').innerHTML += '<p>' + description + '</p>'; var comment = document.getElementById('textareaCode').innerHTML; alert (comment); 
 <textarea id="textareaCode">existing text</textarea> 

Are you sure this isn't working as expected? 您确定这没有按预期进行吗? First you alert 'abc' which is an element. 首先,您提醒'abc'这是一个元素。 So the alert tells you that it's an element. 因此,警报告诉您这是一个元素。 After that you do alert(comment), which should be the alert you're looking for. 之后,您将发出警报(注释),这应该是您要寻找的警报。

 var description ="hello world! love coding"; document.getElementById("textareaCode").value=description; var comment = document.getElementById('textareaCode').value; alert (comment) 
 <textarea id="textareaCode"></textarea/> 
u cant append a html element to a textarea instead u can do it with value ! 您不能将html元素附加到textarea,而可以使用value做到这一点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM