How to generate a DOM element with jQuery

How to create a DOM element using jQuery.

  1. Part 1 (how in the old version)
  2. Part 2 (version 1.4 or later)

Part 1 (how in the old version)

These steps would be.

  1. to generate a jQuery object
  2. attributes, such as adding a jQuery object
  3. Get elements and registering a jQuery object as a child element

And in order to do the following. Furthermore, no problem as \"jQuery\" part of the \"$\". You can chain methods.

var objtest = $('<a>');//<a></a>
objtest.attr('href', '');//<a href="'"></a>
objtest.text('example');//<a href="'">example</a>
objtest.appendTo( document.body );//<body><a href="'">example</a></body>


Part 2 (version 1.4 or later)

Above part 1-1.2 to the dash.

$('<a>', { href: '',
           text: 'example' } )
.appendTo( document.body );

Other, specify width and height, or set checked check box.
For more information, see website (Japan words).



HOMEHOME CategoryCategory HistoryHistory

Site Search :

[HTML link code]