【问题标题】:create and append a span inside multiple div of same class在同一类的多个 div 中创建并附加一个跨度
【发布时间】:2022-12-02 17:13:04
【问题描述】:
<div data-component-id=\"nwv4kv9j5ct9\" class=\"component-inner-container status-red \" data-component-status=\"major_outage\" data-js-hook=\"\" id=\"nwv4kv9j5ct9\">

   <span class=\"name table-item-name\">JHP</span>
   <span class=\"service-tier table-item-tier\"><a href=\"\" target=\"_blank\" 
      onclick=\"event.stopPropagation()\">Tier 2</a></span>
 
   <span class=\"tooltip-base tool tooltipstered\" style=\"display: none;\">?</span>
 
   <span class=\"component-status table-item-status-childs\" title=\"\"><span class=\"app-status bg- 
      inactive\">failing</span></span>
 
   <span class=\"tool icon-indicator fa fa-times tooltipstered\"></span>
 
</div>
<div data-component-id=\"rtv4kv9j5cyu\" class=\"component-inner-container status-red \" data-component-status=\"major_outage\" data-js-hook=\"\" id=\"nwv4kv9j5ct9\">

   <span class=\"name table-item-name\">PHS</span>
   <span class=\"service-tier table-item-tier\"><a href=\"\" target=\"_blank\" 
       onclick=\"event.stopPropagation()\">Tier 2</a></span>
 
   <span class=\"tooltip-base tool tooltipstered\" style=\"display: none;\">?</span>
 
   <span class=\"component-status table-item-status-childs\" title=\"\"><span class=\"app-status bg-inactive\">degrading</span></span>
 
   <span class=\"tool icon-indicator fa fa-times tooltipstered\"></span>
 
</div>
......
......

I need to create and append a span tag &lt;span class=\"type table-item-type\"&gt;Type&lt;/span&gt; inside each div with class name component-inner-container. It should be after

$(\'.component-inner-container\').each(function () {
        var span = $(\'<span />\').attr(\'className\', \'type\')
        span.appendTo(\".component-inner-container\");

    });

I\'m stuck here and couldn\'t able to find right approach

Edit : added span should be after for example &lt;span class=\"name table-item-name\"&gt;PHS&lt;/span&gt;

  • and the jquery code you\'re using isn\'t working? if so is it crashing or anything else?
  • it is not working
  • did you try using $(\'.component-inner-container\').append(span); instead of span.appendTo(\"...\")

标签: javascript jquery


【解决方案1】:
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多