Creating a div element in jQuery

1.7k2009 年提問 · useranon ·javascriptjqueryhtmlappendjquery-append 主題:DOM、事件、表單與 jQuery → 在 Stack Overflow 看原文 ↗

How do I create a div element in jQuery?

重點答案

1.2k採納 by cm2 原文 ↗

You can use append (to add at last position of parent) or prepend (to add at fist position of parent):

$('#parent').append('<div>hello</div>');    
// or
$('<div>hello</div>').appendTo('#parent');

Alternatively, you can use the .html() or .add() as mentioned in a different answer.

Technically $('<div></div>') will 'create' a div element (or more specifically a DIV DOM element) but won't add it to your HTML document. You will then need to use that in combination with the other answers to actually do anything useful with it (such as using the append() method or such like).

The manipulation documentation gives you all the various options on how to add new elements.