2013-05-28 42 views
0

我要追加mytext的DIV中格內,但它正在更新的div以外文本如何追加DIV其他分區

<head> 
<script type="text/javascript" src="jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(function(){ 
var jj= $('body').append('<div class="mytext">') 
jj.append('<div>min</div>') 
jj.append('</div>') 
}) 
</script> 
</head> 
<body> 
</body> 
+2

http://stackoverflow.com/questions/8316940/javascript-appending-another-div-data-on-div-tag-dynamically – Nitesh

回答

3

jj表示「body」元素。

$(function() { 
    var $mydiv = $('<div>min</div>'), 
     $body = $('body'); 
    $body.append('<div class="mytext">'); 
    var $mytext = $body.find('.mytext:last'); 
    $mytext.append($mydiv); 
}) 
+0

因爲OP想要做更復雜的事情 –

1

嘗試這樣

$(function(){ 
    var jj= $('body').append('<div class="mytext"></div>'); 
    $('.mytext').html('<div>min</div>') ;  
}); 
+1

你試過嗎??....然後第一嘗試downvote它 – Gautam3164

+1

雅,爲什麼downvote ??? –

+0

Iam確定它會工作,但我不知道他爲什麼低估了它 – Gautam3164

0

你不需要JJ

<head> 
<script type="text/javascript" src="jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(function(){ 
    $('body').append('<div class="mytext"><div>min</div></div>') 
}) 
</script> 
</head> 
<body> 
</body> 
+0

是的正確,因爲'

min
'是這樣一個動態元素 – Ted

+0

但@泰德它沒有錯的答對... ??所以我不認爲downvote ...即使它比你的答更好,即使 – Gautam3164

+0

@ Gautam3164 - 所以相比之下,我的答案值得downvote,但你的不?使用你的邏輯,他們都工作......所以如果我發佈了一個解決方案,使用循環並繞過DOM 1000次,但仍然添加了一個div到另一個,我應該得到一個+1 ... – Ted

0
var myDiv = $('<div class="mytext"></div>'); 
myDiv.append('<div>min</div>'); 
$('body').append(myDiv); 

append功能不追加的HTML文檔。它將元素附加到文檔對象樹中。

但是,如果你願意,你可以把它寫在這樣的一行上。

$('body').append('<div class="mytext"><div>min</div></div>'); 

繁榮。

0
$(function(){ 
    var jj= $('<div class="mytext"></div>'); 
    body.append(jj.append('<div>min</div>')); 
});