开发者

Loop to create multiple div elements? JQUERY

Any idea what's wrong with my code?

var pageLimit=30;

$(document).ready(function() {
 for(var i = 1; i <= pageLimit; i++) {
  $('#test').append('<div id="page' + i + '" class="touch">TESTING</ div>' )
 }
}

What I want is to have that function create as many divs in the body as the pageLimit value. So if someone were to go into the code and change the pageLimit to 50, it would create 50 div tags.

in the body tags, all I have is the test div. I wanted to put it into the body, without inserting it into any other divs. So I tried to replace #test with body, didn't work.

Please help! Thanks.

EDIT:开发者_运维百科 Sorry, I have the ); in my original code I just forgot to copy it here! Yeah, the

tags were before I knew how to insert code into this... lol Sorry. I have ); in my original code, it still doesn't work.


Missing ");" after the last }.

$(document).ready(function () {
    for (var i = 1; i <= pageLimit; i++) {
        $('#test').append('TESTING');
    } 
});


http://jsfiddle.net/Q6Lnw/2/

You were missing the end of the ready function's )


Your issue is a simple syntax problem. You were missing ")". Always make sure to add in line endings too. This works:

$(document).ready(function () {
         for (var i = 1; i <= pageLimit; i++) {
             $('#test').append('TESTING');
         }
     });


$('body').append('<div>TESTING</div>')

Should work. What does your not-working code look like?


How about document.body.innerHTML += 'TESTING'; ?


It seems it's still a syntax problem, you have simple quotes then back quotes in your element string, try removing the backquotes. And of course, make sure you have <div id="test"></div> in your html.

$(document).ready(function() {
   for(var i = 1; i <= pageLimit; i++) {
     $('#test').append('<div id="page' + i + '" class="touch">TESTING</div>' )
   }
});

Unless that's another typo in your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜