jQuery - 附加一个元素特定次数?

发布于 2024-11-15 02:24:26 字数 1054 浏览 3 评论 0原文

可以说,在这种情况下,我有 4 个名为“.CountThese”的 div,我只想这样做,获取该特定类的元素数量,然后附加一个 div 的次数与“.CountThese”的数量(在 this 情况下为 4 次)

这是 html:

<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>

<div class="appendHere"></div>

这将是结果:

<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>

<div class="appendHere">
   <div class="appendedDIVs"></div>
   <div class="appendedDIVs"></div>
   <div class="appendedDIVs"></div>
   <div class="appendedDIVs"></div>
</div>

到目前为止,我发现您可以计算这样的元素数量:

function divcount() {
   var Count =  $('.CountThese').length(); 
   document.write(Count)
}

以为我不一定知道如何使用那..我真的不知道如何使用该数字来附加div或其他任何与此相关的东西..

如何实现这一点?

Lets say that in this case i have 4 divs called ".CountThese" and i wanted to do just that, get the number of elements with that specific class and then append a div as many times as was the amount of ".CountThese" ( 4 times in this case )

This is the html:

<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>

<div class="appendHere"></div>

This would be the result:

<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>

<div class="appendHere">
   <div class="appendedDIVs"></div>
   <div class="appendedDIVs"></div>
   <div class="appendedDIVs"></div>
   <div class="appendedDIVs"></div>
</div>

I got only so far that i found out that you can count number of elements something like this:

function divcount() {
   var Count =  $('.CountThese').length(); 
   document.write(Count)
}

thought i dont necessarily know how to use that.. i seriously have no idea how i could then use that number to append divs or anything else for that matter..

How can this be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想念有你 2024-11-22 02:24:26

您可以使用数组 来避免手动迭代join() 方法如下:

$(".appendHere").append(new Array(++n).join('<div class="appendedDivs">New!</div>'));

其中 n 是要创建的元素数量。

这比接受的答案更好,因为它只操作 DOM 一次而不是 n 次。


var n = $(".CountThese").length;
$(".appendHere").append(new Array(++n).join('<div class="appendedDivs">New!</div>'));
.CountThese {
  display: inline-block;
  width: 46px;
  height: 50px;
  background: royalblue;
}
.appendedDivs {
  display: inline-block;
  height: 30px;
  line-height: 30px;
  padding: 5px;
  margin-right:5px;
  color: gold;
  font-weight: bold;
  background: dodgerblue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>

<div class="appendHere"></div>

You can avoid the manual iteration by using the array join() method like:

$(".appendHere").append(new Array(++n).join('<div class="appendedDivs">New!</div>'));

Where n is the number of elements you want to create.

This is better than the accepted answer since it only manipulates the DOM once rather than n times.


var n = $(".CountThese").length;
$(".appendHere").append(new Array(++n).join('<div class="appendedDivs">New!</div>'));
.CountThese {
  display: inline-block;
  width: 46px;
  height: 50px;
  background: royalblue;
}
.appendedDivs {
  display: inline-block;
  height: 30px;
  line-height: 30px;
  padding: 5px;
  margin-right:5px;
  color: gold;
  font-weight: bold;
  background: dodgerblue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>
<div class="CountThese"></div>

<div class="appendHere"></div>

吃素的狼 2024-11-22 02:24:26

您可以迭代 .CountThese div 并将每个 div 附加到 .appendHere

$('.CountThese').each(function(){
  $('.appendHere').append('<div class="appendedDivs"></div>');
});

You can iterate over the .CountThese divs and for each one, append to .appendHere

$('.CountThese').each(function(){
  $('.appendHere').append('<div class="appendedDivs"></div>');
});
腹黑女流氓 2024-11-22 02:24:26

最好的方法是迭代每个 .CountThese div,就像 Don 和 Christopher 提到的那样。不过,如果你想按照自己的方式来做,你可以这样做:

var count = $('.CountThese').size();
for (var i=0; i<count; i++){
    $('div.appendHere').append('<div class="appendedDIVs"></div>');
}

The best way to do it would be to iterate over each .CountThese divs like Don and Christopher mentioned. If you wanted to do it your way, though, you could do it like this:

var count = $('.CountThese').size();
for (var i=0; i<count; i++){
    $('div.appendHere').append('<div class="appendedDIVs"></div>');
}
森林散布 2024-11-22 02:24:26
$('.CountThese').each(function() {
    $('div.appendHere').append('<div class="appendedDIVs"></div>');
});
$('.CountThese').each(function() {
    $('div.appendHere').append('<div class="appendedDIVs"></div>');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文