jQuery 循环创建多个 div 元素?查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3265707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:08:20  来源:igfitidea点击:

Loop to create multiple div elements? JQUERY

jqueryloopshtml

提问by Annie

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.

我想要的是让该函数在正文中创建与 pageLimit 值一样多的 div。因此,如果有人要进入代码并将 pageLimit 更改为 50,它将创建 50 个 div 标签。

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.

在正文标签中,我所拥有的只是测试 div。我想将它放入正文中,而不将其插入任何其他 div。所以我试图用 body 替换 #test,没有用。

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.

编辑:对不起,我有 ); 在我的原始代码中,我只是忘记在这里复制它!是的,
标签是在我知道如何将代码插入到这个之前...大声笑对不起。我有 ); 在我的原始代码中,它仍然不起作用。

采纳答案by Zacho

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');
         }
     });

回答by Andy Robinson

Missing ");" after the last }.

丢失的 ”);” 在最后一个} 之后。

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

回答by Ian Wetherbee

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

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

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

你错过了就绪函数的结尾 )

回答by Lourens

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

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

应该管用。你不工作的代码是什么样的?

回答by guzart

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.

似乎它仍然是一个语法问题,您在元素字符串中有简单的引号然后反引号,请尝试删除反引号。当然,请确保您<div id="test"></div>的 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.

除非这是您问题中的另一个错字。

回答by Fosco

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

document.body.innerHTML += 'TESTING' 怎么样??