[英]adding property to array element in javascript
I'm trying to learn and understand javascript. 我正在尝试学习和理解javascript。
what is wrong with the following code? 以下代码有什么问题?
var d=[];
d[0]=document.createElement('div');
d[0].title=document.createElement('div');
d[0].appendChild(d[0].title);
I get this error: TypeError: Argument 1 of Node.appendChild is not an object. 我收到此错误:TypeError:Node.appendChild的参数1不是对象。
Can you suggest a solution? 您能提出解决方案吗?
This line d[0].appendChild(d[0].title);
这行d[0].appendChild(d[0].title);
is expecting an element to be appended to the div. 期望将元素追加到div。 Your simply appending a text node. 您只需附加一个文本节点。 Create another div
(or whatever element you want) and append that. 创建另一个div
(或所需的任何元素)并追加。
The problem is that the name title
is reserved. 问题在于名称title
是保留的。 Try a different name. 尝试使用其他名称。
.title
is an attribute of the element, which is a string. .title
是元素的属性,它是一个字符串。 When you try to append something to that attribute it is expecting a string. 当您尝试向该属性添加内容时,它需要一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.