{"id":4509,"date":"2013-04-06T13:21:45","date_gmt":"2013-04-06T04:21:45","guid":{"rendered":"http:\/\/www.webcyou.com\/?p=4509"},"modified":"2013-04-06T14:17:39","modified_gmt":"2013-04-06T05:17:39","slug":"%e3%82%b9%e3%83%9e%e3%83%bc%e3%83%88%e3%83%95%e3%82%a9%e3%83%b3ui-%e8%a6%81%e7%b4%a0%e4%b8%a6%e3%81%b3%e6%9b%bf%e3%81%88%ef%bc%88%e9%85%8d%e5%88%97%e8%a4%87%e8%a3%bd%ef%bc%89javascript-clonenode","status":"publish","type":"post","link":"https:\/\/www.webcyou.com\/?p=4509","title":{"rendered":"\u30b9\u30de\u30fc\u30c8\u30d5\u30a9\u30f3UI \u8981\u7d20\u4e26\u3073\u66ff\u3048\uff08\u914d\u5217\u8907\u88fd\uff09JavaScript cloneNode"},"content":{"rendered":"<p>\u6700\u8fd1\u826f\u304f\u3042\u308b\u306a\u3068\u601d\u3046\u306e\u304c\u3001\u300c\u8981\u7d20(DOM)\u3092\u5165\u308c\u66ff\u3048\u305f\u3044\u300d\u3068\u8a00\u3063\u305f\u8981\u4ef6\u3060\u3063\u305f\u308a\u3082\u3057\u307e\u3059\u3002<\/p>\n<p>\u30bd\u30fc\u30b7\u30e3\u30eb\u30b2\u30fc\u30e0\u306a\u3093\u304b\u306b\u306a\u308b\u3068\u30ab\u30fc\u30c9\u753b\u50cf\u3092\u5165\u66ff\u3048\u3002\u3068\u304b\u306b\u306a\u308a\u307e\u3059\u304b\u306d\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u69d8\u306a\u4e26\u3073\u3060\u3068\u3057\u3066\u3001<\/p>\n<p><a href=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/card.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/card.png\" alt=\"\" title=\"card\" width=\"405\" height=\"143\" class=\"alignnone size-full wp-image-4511\" srcset=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/card.png 405w, https:\/\/www.webcyou.com\/wp-content\/uploads\/card-300x105.png 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/a><\/p>\n<p>\u30bf\u30c3\u30d7\u2192\u30bf\u30c3\u30d7\u3067\u5165\u66ff\u3048\u3002\u3068\u304b\u3001\u30bf\u30c3\u30d7\u2192\u30b9\u30e9\u30a4\u30c9\u3067\u5165\u66ff\u3048\u3002<\/p>\n<p>\u3060\u3063\u305f\u308a\u3002<\/p>\n<p>\u305d\u3093\u306a\u3068\u304d\u3001JavaScript \u3067cloneNode \u3092\u4f7f\u3048\u3070\u5b9f\u73fe\u306f\u53ef\u80fd\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u30bf\u30c3\u30c1\u3084\u30af\u30ea\u30c3\u30af\u3067\u8981\u7d20\u3092elemA\u3001elemB\u306e\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">var\u00a0\u00a0newElemA = elemA.cloneNode(true),\r\n   \u00a0 \u00a0 \u00a0 \u00a0 newElemB = elemB.cloneNode(true);\r\nelemA.parentNode.replaceChild( newElemB, elemA\u00a0);\r\nelemB.parentNode.replaceChild( newElemA, \u00a0elemB.\u00a0);<\/pre>\n<div>\u3053\u3061\u3089\u3067\u5165\u66ff\u3048\u53ef\u80fd\u3068\u306a\u308a\u307e\u3059\u3002<\/div>\n<p>\u3061\u306a\u307f\u306breplaceChild\u306b\u95a2\u3057\u3066\u306f\u4ee5\u4e0b\u306e\u69d8\u306b\u7b2c\u4e00\u5f15\u6570\u306b\u65b0\u3057\u3044\u8981\u7d20\u3001\u7b2c\u4e8c\u5f15\u6570\u306b\u53e4\u3044\u8981\u7d20\u3092\u4ee3\u5165\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">replacedNode = parentNode.replaceChild(newChild, oldChild);<\/pre>\n<p><strong>newChild:\u00a0oldChild\u00a0\u3092\u7f6e\u304d\u63db\u3048\u308b\u65b0\u3057\u3044\u30ce\u30fc\u30c9<\/strong><\/p>\n<p><strong>oldChild:\u00a0newChild\u00a0\u306b\u7f6e\u304d\u63db\u3048\u308b\u30ce\u30fc<\/strong>\u30c9<\/p>\n<p><strong>replacedNode: \u7f6e\u304d\u63db\u3048\u3089\u308c\u305f\u30ce\u30fc\u30c9<\/strong><\/p>\n<p>\u3068\u3053\u3053\u307e\u3067\u306f\u3001\u554f\u984c\u306a\u3044\u304b\u3068\u601d\u308f\u308c\u307e\u3059\u304c\u3001\u8907\u6570clone\u3057\u305f\u3044\u3002\u3064\u307e\u308a\u914d\u5217\u3067clone\u3057\u305f\u3044\u3063\u3066\u306a\u3063\u305f\u6642\u3001<\/p>\n<p>\u30b7\u30f3\u30d7\u30eb\u306a\u914d\u5217\u3067\u3042\u308c\u3070\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvar defaultItem,cloneItem;\r\ndefaultItem = &#x5B;&quot;a&quot;,&quot;b&quot;,&quot;c&quot;];\r\ncloneItem = &#x5B;];\r\nfor (var i=0, l = defaultItem.length; \u00a0i &lt; l; \u00a0i++) {\r\n\u00a0 cloneItem&#x5B;i] = defaultItem&#x5B;i];\r\n}\r\n&lt;p&gt;console.log(cloneItem);&lt;\/p&gt;\r\n<\/pre>\n<p><strong>console<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&#x5B;\"a\",\"b\",\"c\"]<\/pre>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvar defaultItem,cloneItem;\r\ndefaultItem = &#x5B;&quot;a&quot;,&quot;b&quot;,&quot;c&quot;];\r\ncloneItem = &#x5B;];\r\nfor (var i = 0, l = defaultItem.length; i &lt; l; i++) {\r\n cloneItem&#x5B;i] = defaultItem&#x5B;i];\r\n}\r\ndefaultItem&#x5B;3] = &quot;d&quot;;\r\ncloneItem&#x5B;3] = &quot;e&quot;;\r\n\r\nconsole.log(defaultItem);\r\nconsole.log(cloneItem);\r\n<\/pre>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&#x5B;\"a\",\"b\",\"c\",\"d\"]&#x5B;\"a\",\"b\",\"c\",\"e\"]<\/pre>\n<p>\u3068\u3067\u304d\u307e\u3059\u304c\u3001\u30af\u30ed\u30fc\u30f3\u3059\u308b\u3068\u306a\u308b\u3068\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvar defaultItem = &#x5B;&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;],\r\n    cloneItem;\r\nfor (var i = 0, l = defaultItem.length; i &lt; l; i++) {\r\n  cloneItem = item&#x5B;i].cloneNode(true);\r\n}\r\n<\/pre>\n<p>\u3053\u3093\u306a\u98a8\u306bfor\u6587\u3067\u307e\u308f\u3057\u3066\u3082\u30b3\u30d4\u30fc\u3055\u308c\u306a\u3044\u308f\u3051\u3067\u3001\u3001\u3001\u56f0\u3063\u305f\u3061\u3083\u3093\u3067\u3059\u306d\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001Array\u306ePrototype\u306bclone\u3092\u8ffd\u52a0\u3057\u3001\u518d\u8d77\u3055\u305b\u308b\u3084\u308a\u65b9\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray.prototype.clone = function() {\r\n  if ( this&#x5B;0].constructor == Array ) {\r\n    var arr, i;\r\n    arr = new Array( this.length );\r\n    for ( i = 0; i &lt; ar.length; i++ ) {\r\n      arr&#x5B;i] = this&#x5B;i].clone();\r\n    }\r\n    return arr;\r\n }\r\n return Array.apply( null, this );\r\n}<\/pre>\n<p>\u81ea\u5206\u81ea\u8eab\uff08\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\uff09\u304c\u914d\u5217\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3001\u65b0\u3057\u3044\u914d\u5217\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u914d\u5217\u306e\u6570\u3060\u3051\u66f4\u306b\u914d\u5217\u3068\u3057\u3066\u4f5c\u6210\u3002\u305d\u308c\u3092\u8fd4\u3059\u4e8b\u306b\u3088\u3063\u3066\u8907\u6570\uff08\u914d\u5217\u3068\u3057\u3066\uff09clone\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n<p>apply\u306fcall\u3068\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001this\u306e\u5024\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<code>null<\/code>\u00a0\u304b\u00a0<code>undefined<\/code>\u00a0\u304c\u6307\u5b9a\u3055\u308c\u305f\u5834\u5408\u306f\u00a0<code>this<\/code>\u00a0\u306e\u5024\u306f global object \u306b\u306a\u308a\u307e\u3059\u306e\u3067\u3001clone\u81ea\u8eab\u3082\u30b0\u30ed\u30fc\u30d0\u30eb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u6271\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u30fc\uff01<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">var cloneItem = new Array();\r\n    defaultItem = &#x5B;\"a\",\"b\",\"c\",\"d\"];\r\n    cloneItem = defaultItem.clone();\r\n    console.log(cloneItem);<\/pre>\n<p><a href=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/bbc6d875f9d589696b048b2c2b2c4873.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/bbc6d875f9d589696b048b2c2b2c4873.png\" alt=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2013-04-06 2.30.46\" width=\"297\" height=\"49\" class=\"alignnone size-full wp-image-4512\" \/><\/a><\/p>\n<p>console.log\u3092\u78ba\u8a8d\u3059\u308b\u3068\u3002\u304a\u3001\u826f\u3044\u611f\u3058\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">Array.prototype.clone = function() {\r\n  if ( this&#x5B;0].constructor == Array ) {\r\n    var arr, i;\r\n        arr = new Array( this.length );\r\n    for ( i = 0; i &lt; ar.length; i++ ) {\r\n      arr&#x5B;i] = this&#x5B;i].clone();\r\n    }\r\n    return arr;\r\n  }\r\n  return Array.apply( null, this );\r\n}\r\n\r\nvar li = document.querySelectorAll(&quot;ul &gt; li&quot;);\r\nvar cloneItem = new Array();\r\n    defaultItem = &#x5B;li];\r\n    cloneItem = defaultItem.clone();\r\n    console.log(cloneItem);<\/pre>\n<p>dom\u3092clone\u3055\u305b\u3066\u307f\u308b\u3068\u3001<\/p>\n<p><a href=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/16cc00e36eb3074a998508c1bdf86352.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/16cc00e36eb3074a998508c1bdf86352.png\" alt=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2013-04-06 13.17.37\" width=\"258\" height=\"160\" class=\"alignnone size-full wp-image-4513\" \/><\/a><\/p>\n<p>\u3068\u3044\u3063\u305f\u611f\u3058\u306b\u914d\u5217\u3067\u30af\u30ed\u30fc\u30f3\u51fa\u6765\u3066\u3044\u308b\u306e\u304c\u78ba\u8a8d\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u826f\u304f\u3042\u308b\u306a\u3068\u601d\u3046\u306e\u304c\u3001\u300c\u8981\u7d20(DOM)\u3092\u5165\u308c\u66ff\u3048\u305f\u3044\u300d\u3068\u8a00\u3063\u305f\u8981\u4ef6\u3060\u3063\u305f\u308a\u3082\u3057\u307e\u3059\u3002 \u30bd\u30fc\u30b7\u30e3\u30eb\u30b2\u30fc\u30e0\u306a\u3093\u304b\u306b\u306a\u308b\u3068\u30ab\u30fc\u30c9\u753b\u50cf\u3092\u5165\u66ff\u3048\u3002\u3068\u304b\u306b\u306a\u308a\u307e\u3059\u304b\u306d\u3002 \u4ee5\u4e0b\u306e\u69d8\u306a\u4e26\u3073\u3060\u3068\u3057\u3066\u3001 \u30bf\u30c3\u30d7\u2192\u30bf\u30c3\u30d7\u3067\u5165\u66ff\u3048\u3002\u3068\u304b\u3001\u30bf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[168,176,40,166,37,47,179,50,46,178],"class_list":["post-4509","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-html","tag-html5","tag-ios-sdk","tag-iphone","tag-iphone-","tag-javascript-2","tag-jquery","tag-jquery-mobile","tag-js","tag-178"],"_links":{"self":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/4509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4509"}],"version-history":[{"count":3,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/4509\/revisions"}],"predecessor-version":[{"id":4515,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/4509\/revisions\/4515"}],"wp:attachment":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}