<div>を作成して動的に<div>を追加


128

内部に<div>追加された動的に作成しようとしています<div>。これまでのところ、うまくいきます:

var iDiv = document.createElement('div');
iDiv.id = 'block';
iDiv.className = 'block';
document.getElementsByTagName('body')[0].appendChild(iDiv);

2番目のdivを作成して最初のdivに追加するのに問題があります。

私は基本的にこれを最後のマークアップとして行いたいです:

<div id="block" class="block">
   <div class="block-2"></div>
</div>

19
これはbody要素を取得する奇妙な方法です...を使用してくださいdocument.body
Guffa

回答:


234

同じプロセスを使用します。作成iDivした元の要素<div id='block'>を参照する変数がすでにあります。別のものを作成して<div>を呼び出すだけですappendChild()

// Your existing code unmodified...
var iDiv = document.createElement('div');
iDiv.id = 'block';
iDiv.className = 'block';
document.getElementsByTagName('body')[0].appendChild(iDiv);

// Now create and append to iDiv
var innerDiv = document.createElement('div');
innerDiv.className = 'block-2';

// The variable iDiv is still good... Just append to it.
iDiv.appendChild(innerDiv);

http://jsfiddle.net/W4Sup/1/

イベントの作成順序は、上記のとおりである必要はありません。innerDiv両方をに追加する前に、新しいを外側のdivに追加することもできます<body>

var iDiv = document.createElement('div');
iDiv.id = 'block';
iDiv.className = 'block';

// Create the inner div before appending to the body
var innerDiv = document.createElement('div');
innerDiv.className = 'block-2';

// The variable iDiv is still good... Just append to it.
iDiv.appendChild(innerDiv);

// Then append the whole thing onto the body
document.getElementsByTagName('body')[0].appendChild(iDiv);

3
「余分なマイル」に行くことは物語の教訓でした:-)!

次のようにして最終行を短くすることもできます:document.body.appendChild(iDiv);
idan hav

8
var iDiv = document.createElement('div');

iDiv.id = 'block';
iDiv.className = 'block';
document.getElementsByTagName('body')[0].appendChild(iDiv);

var div2 = document.createElement('div');

div2.className = 'block-2';
iDiv.appendChild(div2);

Michael Berkowski
Oliver Bayes-Shelton

3
私はそれができなかったと思います。単純な解は通常、分散が小さい:D。
Moazzam Khan

4
var iDiv = document.createElement('div'),
    jDiv = document.createElement('div');
iDiv.id = 'block';
iDiv.className = 'block';
jDiv.className = 'block-2';
iDiv.appendChild(jDiv);
document.getElementsByTagName('body')[0].appendChild(iDiv);

3

まあ、これがどれほど動的であるかはわかりませんが、デバッグの命を救うことがあります。

var daString="<div id=\'block\' class=\'block\'><div class=\'block-2\'></div></div>";
var daParent=document.getElementById("the ID of whatever your parent is goes in here");
daParent.innerHTML=daString;

「Rat javascript」私が正しくやれば。もちろん、divとコンテンツ自体が動的でない場合は直接機能します。または、文字列を操作して変更することもできます。ただし、文字列の操作は「element.property = bla」アプローチよりも複雑ですが、これは非常に歓迎されます柔軟性、そして素晴らしいデバッグツールでもあります:)それが役に立てば幸いです。


2
var i=0,length=2;

     for(i; i<=length;i++)
 {
  $('#footer-div'+[i]).append($('<div class="ui-footer ui-bar-b ui-footer-fixed slideup" data-theme="b" data-position="fixed" data-role="footer" role="contentinfo" ><h3 class="ui-title" role="heading" aria-level="1">Advertisement </h3></div>')); 

 }

JavaScriptライブラリを使用していて、例では元の質問とは異なるマークアップを使用していることを示しているとよいでしょう。
rob

2
window.onload = function() {
  var iDiv = document.createElement('div');
  iDiv.id = 'block';
  iDiv.className = 'block';
  document.body.appendChild(iDiv);

  var iiDiv = document.createElement('div');
  iiDiv.className = 'block-2';

  var s = document.getElementById('block');
  s.appendChild(iiDiv);
}

1
var arrayDiv = new Array();
    for(var i=0; i <= 1; i++){
        arrayDiv[i] = document.createElement('div');
        arrayDiv[i].id = 'block' + i;
        arrayDiv[i].className = 'block' + i;
    }
    document.body.appendChild(arrayDiv[0].appendChild(arrayDiv[1]));

0
var iDiv = document.createElement('div');
iDiv.id = 'block';
iDiv.className = 'block';

var iDiv2 = document.createElement('div');
iDiv2.className = "block-2";

iDiv.appendChild(iDiv2);
// Append to the document last so that the first append is more efficient.
document.body.appendChild(iDiv);

0
    while(i<10){
               $('#Postsoutput').prepend('<div id="first'+i+'">'+i+'</div>');
               /* get the dynamic Div*/
               $('#first'+i).hide(1000);
               $('#first'+i).show(1000);
                i++;
                }
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.