简体   繁体   English

jQuery.append()在类选择器的特定位置

[英]Jquery.append() in certain position of class selector

I have this jQuery code: 我有这个jQuery代码:

var $collapsibleProducts = $('.collapsible-body');
if ($collapsibleProducts.length != 0) {
  $.each($('.collapsible-body'), function (i) { 
    if ($('.collapsible-body')[i].children.length === 0) {
      $('.collapsible-body')[i].append("<span class='something'>something</span>")
    }
  });
}

But I only getting inside my div.collapsible-body this string "<span class='something'>something</span>" instead of html <span> tag with 'something' string. 但是,我只能在div.collapsible-body此字符串"<span class='something'>something</span>"而不是带有<span> something'字符串的html <span>标记。

Like this (image) 这样(图片)

Is there something I doing wrong? 我做错什么了吗? or is there another way to do it? 还是有另一种方法?

No need to check for the length (jQuery's easy like that), and you're using the wrong kind of each. 无需检查长度(jQuery很容易做到这一点),并且每种类型使用的类型都不正确。 You're using the general iterator whereas jQuery has a special iterator for jQuery objects . 您使用的是通用迭代器,而jQuery 为jQuery对象提供了特殊的迭代器

var $collapsibleProducts = $('.collapsible-body');

$collapsibleProducts.each(function(i, el) {
  if ($(el).children().length === 0) {
    $(el).append("<span class='something'>something</span>");
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM