最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Making an array of HTML elements - Stack Overflow

programmeradmin5浏览0评论

I want to make an array of HTML elements. I have tried the following:

array.push(<tr>);
array.push(<input type="text">);
array.push(/tr);

What is the correct way to do this?

I want to make an array of HTML elements. I have tried the following:

array.push(<tr>);
array.push(<input type="text">);
array.push(/tr);

What is the correct way to do this?

Share Improve this question edited Jul 26, 2018 at 19:02 Brad 163k55 gold badges377 silver badges552 bronze badges asked Jul 26, 2018 at 18:42 user395817user395817 3052 gold badges4 silver badges12 bronze badges 13
  • 2 yes possible. array.push("<tr>") – Hari Das Commented Jul 26, 2018 at 18:44
  • 2 Why is this getting downvotes? – Brandon Dixon Commented Jul 26, 2018 at 18:44
  • 4 @BrandonDixon Perhaps because of the fact that entering that code into any browser's console or other REPL would show errors which show that the problem is? The tooltip on the downvote icon says "This question does not show any research effort; it is unclear or not useful.". But it could just be the way the wind is blowing. – Heretic Monkey Commented Jul 26, 2018 at 18:51
  • 1 @HereticMonkey The person asking the question clearly doesn't know that, or they would have done it already. This is a perfectly valid question. – Brad Commented Jul 26, 2018 at 18:52
  • 2 @HereticMonkey before you bow out, understand that if someone gets enough downvotes on enough questions, he/she is banned from asking further questions. Thus, it is inescapably akin to punishment. – Brandon Dixon Commented Jul 26, 2018 at 22:18
 |  Show 8 more ments

3 Answers 3

Reset to default 6

There are a few ways to go about this.

The first way is to store the tag as a string:

array.push("<tr><input type='text'></tr>");

Or alternatively, three different strings.

The second way is to actually create an HTML element and add it. For example:

let te = document.createElement('tr');
let ti = document.createElement('input');
input.type = 'text'; //the syntax might be slightly different here
te.appendChild(ti);

This isn't really an array, but it is a similar concept. If you want to put that element onto the body, you can use:

document.body.appendChild(te);

Depending on what you want to do, you can use one of the following options:

  1. Insert it as a string. For example, array.push('<tr><input type="text"></tr>').
  2. Insert it as a DOM element, created by document.createElement(). This is useful when you want to insert it to the DOM after. For example:
var el = document.createElement('tr');
var child = document.createElement('input');
child.setAttribute('type', 'text');
el.appendChild(child);
array.push(el);

For more information about document.createElement(), see MDN Docs.

It is possible, just treat the data as a string by adding the quotes:

var array = [];

array.push('<tr>');
array.push('<input type="text">');
array.push('</tr>');


console.log(array);

发布评论

评论列表(0)

  1. 暂无评论