试图做一个jQuery的'循环,在另一个div添加div元素。 [英] Trying to make a jquery 'for' loop that adds div elements inside another div.
问题描述
<!DOCTYPE html>
< html>
< head>
< link rel ='stylesheet'type ='text / css'href ='etch_a_sketch.css'/>
< script type ='text / javascript'src ='etch_a_sketch.js'>< / script>
< / head>
< body>
< div class =outer>
< / div>
< / body>
< / html>
JS:
< (i = 0; i <16; i ++){
$(')的函数$(
$) < div class =inner>< / div>)。appendTo('。outer');
}
)};
/ pre>
您好!我尝试过在这里和其他地方寻找答案,但没有运气。我试图做一个jQuery的'循环,将动态地在一个外部的div容器中创建16个div元素,代码看起来对我来说很合理,但是它不工作,我没有发布CSS,因为它是无关紧要的。首先,你有语法错误,最后一行
)};
应该是}) ;
。
下一步。没有必要两次创建一个jQuery对象(还有一个语法 -
}
应该是})
)。 >
这行:
$(document).ready(function(){
与此行完全相同:
$(function(){
所以,总之,你最终应该是这样的:
$ b $ pre $ $ $ $ $(document) .ready(function(){
for(i = 0; i <16; i ++){
$('< div class =inner> blah< / div> ('.outer');
}
});
或this :
$(function(){
for(i = 0; i <16; i ++){
$('< div class =inner> blah< / div>')。appendTo('。outer');
}
});
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' type='text/css' href='etch_a_sketch.css'/>
<script type='text/javascript' src='etch_a_sketch.js'></script>
</head>
<body>
<div class="outer">
</div>
</body>
</html>
JS:
$(document).ready(function() {
$(function() {
for(i=0; i<16; i++) {
$('<div class="inner"></div>').appendTo('.outer');
}
)};
Hello guys! I've tried looking for an answer here and elsewhere but with no luck. I'm trying to make a jquery 'for' loop that will dynamically make 16 div elements within an outer div container. The code looks sound to me but it's not working. I didn't post the CSS because it's irrelevant. Any help would be much appreciated!
First. You have syntax errors. Last line )};
should be });
.
Next. No need to create a jQuery object twice (there's a syntax too - }
should be })
).
This line:
$(document).ready(function() {
does the exact same thing as this line:
$(function() {
So, in summary, you should end up either with this:
$(document).ready(function() {
for(i=0; i<16; i++) {
$('<div class="inner">blah</div>').appendTo('.outer');
}
});
or this:
$(function() {
for(i=0; i<16; i++) {
$('<div class="inner">blah</div>').appendTo('.outer');
}
});
这篇关于试图做一个jQuery的'循环,在另一个div添加div元素。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!