jQuery APIリファレンスDOM制御・更新処理:コピー
- clone()〔DOM要素のコピーを作成〕
- clone(true)〔DOM要素をイベントごとコピー〕
clone()
DOM要素のコピーを作成
2009/2/27
clone() 戻り値:jQuery
マッチしたDOM要素をコピーして、コピー元の要素と同じ新しいDOM要素を作成します。 これは、DOM要素内で、別の場所に要素のコピーを移動するのに有用です。

clone()の使用例サンプルを見る
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="ja" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="imagetoolbar" content="no" /> <title>DOM制御・更新処理:コピー:clone()の使用例 | jQuery</title> <link rel="stylesheet" type="text/css" href="/content/lib/global.css" /> <!-- JS --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ /* ボタンがクリックされた時に、ID名が「photo」の要素の中にimg要素をすべてコピーして追加 */ $("#btn_clone").click(function(){ $("#photo img").clone().prependTo("#photo"); }); }); </script> <style type="text/css"> #photo { padding:10px; background-color:#000; } #photo img { border:1px solid #fff; overflow:hidden; margin:10px;} </style> </head> <body> <div id="wrap"> <h1>DOM制御・更新処理:コピー:clone()の使用例 | jQuery</h1> <p>▼ボタンをクリックすると、ID名が「photo」の要素の中にあるimg要素をすべてコピーして追加します。</p> <!-- CODE --> <p><button id="btn_clone">コピー</button></p> <p id="photo"> <img src="http://farm4.static.flickr.com/3089/3143248598_018daa38eb_t.jpg" width="100" height="75" alt="イルミネーション@サザンテラス" /> </p> <!-- CODE / --> </div> </body> </html>
clone(true)
DOM要素をイベントごとコピー
2009/2/27
clone(true) 戻り値:jQuery
マッチしたDOM要素をその要素が持つイベントも含めてコピーし、コピー元の要素と同じ新しいDOM要素を作成します。 これは、DOM要素内で、別の場所に要素とその要素のイベントのコピーを移動するのに有用です。
第1引数trueには、イベントハンドラもコピーするかどうかを真偽値で指定します。

clone(true)の使用例サンプルを見る
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="ja" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="imagetoolbar" content="no" /> <title>DOM制御・更新処理:コピー:clone(true)の使用例 | jQuery</title> <link rel="stylesheet" type="text/css" href="/content/lib/global.css" /> <!-- JS --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ /* ボタンがクリックされた時に、イベントごとボタン要素をコピー */ $("button").click(function(){ $(this).clone(true).insertAfter(this); }); }); </script> </head> <body> <div id="wrap"> <h1>DOM制御・更新処理:コピー:clone(true)の使用例 | jQuery</h1> <p>▼ボタンをクリックすると、イベントごとボタン要素をコピーます。</p> <!-- CODE --> <p> <button>イベントごと要素をコピー</button> </p> <!-- CODE / --> </div> </body> </html>