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', 'http://example.com/');//<a href="'http://example.com/"></a>
objtest.text('example');//<a href="'http://example.com/">example</a>
objtest.appendTo( document.body );//<body><a href="'http://example.com/">example</a></body>

_^_

Part 2 (version 1.4 or later)

Above part 1-1.2 to the dash.

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

Other, specify width and height, or set checked check box.
For more information, see website (Japan words).
http://idocsq.net/page/223

タグ:JavaScript


HOME2011年の記事jQueryでDOM要素を生成する方法

HOMEHOME CategoryCategory HistoryHistory

Site Search :

[HTML link code]