【问题标题】:Replace all instances of html elements替换 html 元素的所有实例
【发布时间】:2018-01-27 23:27:07
【问题描述】:

我正在尝试删除</div><div class="product-list_row"> 的所有实例并将其替换为<!-- removed -->。但我没有运气让这个工作。

我一直在尝试各种解决方案,有些我根本无法工作,而其他人只会做第一个实例。

我也不能使用正则表达式解决方案,因为这是在我的 Shopify 网站上,而且他们的流动代码不适用于正则表达式。

我一直在用这个:

$(document).ready(function(){
    $('.product-list').html(function(index,html){
        return html.replace('</div><div class="product-list_row">', '<!-- so cool -->');
    });
})
.product-list {
  display:table;
  width:100%;
}
.product-list_row {
  display:table-row;
}
.product-list_item {
  display:inline-block;
  @media screen and (min-width: 1000px) {
    display:table-cell;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product-list">
  <div class="product-list_row">
    <div class="product-list_item">1</div>
    <div class="product-list_item">2</div>
    <div class="product-list_item">3</div>
  </div>
  <div class="product-list_row">
    <div class="product-list_item">4</div>
    <div class="product-list_item">5</div>
    <div class="product-list_item">6</div>
  </div>
  <div class="product-list_row">
    <div class="product-list_item">7</div>
    <div class="product-list_item">8</div>
    <div class="product-list_item">9</div>
  </div>  
</div>

应该是这样的:

.product-list {
  display:table;
  width:100%;
}
.product-list_row {
  display:table-row;
}
.product-list_item {
  display:inline-block;
  @media screen and (min-width: 1000px) {
    display:table-cell;
  }
}
<div class="product-list">              
  <div class="product-list_row">
    <div class="product-list_item">1</div>
    <div class="product-list_item">2</div>
    <div class="product-list_item">3</div>
    <!-- removed -->
    <div class="product-list_item">4</div>
    <div class="product-list_item">5</div>
    <div class="product-list_item">6</div>
    <!-- removed -->
    <div class="product-list_item">7</div>
    <div class="product-list_item">8</div>
    <div class="product-list_item">9</div>
  </div>  
</div>

【问题讨论】:

    标签: javascript jquery html replace replaceall


    【解决方案1】:

    那是因为&lt;/div&gt;&lt;div class="product-list_row"&gt; 之间有空格

    试试这个:

    $(document).ready(function(){
    
        $('.product-list').html(function(index,html){
          return html.replace(/<\/div>\s+<div class="product-list_row">/g, '<!-- so cool -->');
        });
    
    })
    .product-list_row{
      margin: 10px 0;
      background: #eee;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="product-list">              
      <div class="product-list_row">
        <div class="product-list_item">1</div>
        <div class="product-list_item">2</div>
        <div class="product-list_item">3</div>
      </div>
      <div class="product-list_row">
        <div class="product-list_item">4</div>
        <div class="product-list_item">5</div>
        <div class="product-list_item">6</div>
      </div>
      <div class="product-list_row">
        <div class="product-list_item">7</div>
        <div class="product-list_item">8</div>
        <div class="product-list_item">9</div>
      </div>  
    </div>

    至于只替换第一次出现的原因,请看这篇有用的document

    【讨论】:

    • 这看起来是一个很好的解决方案,但是 Shopify 的 Liquid 显然不适用于正则表达式。不过感谢您的解决方案,因为这将是非流动框架的一个很好的参考。
    【解决方案2】:

    使用正则表达式不如使用 dom 方法可靠

    将子元素移动到第一行并在清空后移除整个容器

    var $rows = $('.product-list .product-list_row'),
      $rowsToEmpty = $rows.slice(1);
    
    $rows.first().append($rowsToEmpty.children())
    $rowsToEmpty.remove()
    .product-list_row {
      border: 1px solid #ccc
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="product-list">
      <div class="product-list_row">
        <div class="product-list_item">1</div>
        <div class="product-list_item">2</div>
        <div class="product-list_item">3</div>
      </div>
      <div class="product-list_row">
        <div class="product-list_item">4</div>
        <div class="product-list_item">5</div>
        <div class="product-list_item">6</div>
      </div>
      <div class="product-list_row">
        <div class="product-list_item">7</div>
        <div class="product-list_item">8</div>
        <div class="product-list_item">9</div>
      </div>
    </div>

    【讨论】:

    • 这似乎是一个很好的解决方案,但是在不使用注释“”替换元素时,我失去了反转该代码以替换当满足其他条件时,使用原始 div 元素进行注释。是否可以留下评论来代替被删除的 div?
    • 在执行此操作之前存储状态并能够撤消它的多种方法
    • 我不确定这是如何完成的。你有什么建议吗?
    【解决方案3】:

    最好的方法是使用内置的 jquery 方法unwrap(删除父元素)和wrapInner 为子元素添加父元素。然后javascript就变成了

    $(document).ready(function() {
      $('.product-list_item').unwrap();
      // This line is to put one single product-list_row around the entire thing
      $(".product-list").wrapInner('<div class="product-list_row">');
    });
    

    $(document).ready(function() {
      $('.product-list_item').unwrap();
      $(".product-list").wrapInner('<div class="product-list_row">');
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="product-list">
    
      <div class="product-list_row">
        <div class="product-list_item">1</div>
        <div class="product-list_item">2</div>
        <div class="product-list_item">3</div>
      </div>
      <div class="product-list_row">
        <div class="product-list_item">4</div>
        <div class="product-list_item">5</div>
        <div class="product-list_item">6</div>
      </div>
      <div class="product-list_row">
        <div class="product-list_item">7</div>
        <div class="product-list_item">8</div>
        <div class="product-list_item">9</div>
      </div>
    
    </div>

    【讨论】:

    • 这似乎是一个很好的解决方案,但是在不使用注释“”替换元素时,我失去了反转该代码以替换当满足其他条件时,使用原始 div 元素进行注释。是否可以留下评论来代替被删除的 div?
    【解决方案4】:

    我不得不改变我的想法。而不是从硬编码的元素包装器开始并尝试删除它们并用另一个元素(cmets)替换,而是我应该考虑为每个 X 元素添加包装 div。这就是我最终要得到我需要的东西 - Wrap and unwrap divs responsively。不过感谢您的努力!

    【讨论】:

      【解决方案5】:
      $(document).ready(function() {
          var length = $('.product-list .product-list_row').size();
          $('.product-list .product-list_row').each(function(index, obj) {
              if (length - 1 != index) {
                  $(this).after('<!-- so cool -->');
              }
          });
      })
      

      【讨论】:

      • 虽然这个代码块可能会回答这个问题,但最好能稍微解释一下为什么会这样。
      【解决方案6】:

      class="product-list" 添加id="product"。然后你可以这样做:

      var newDiv = document.getElementById('product').innerHTML;
      var unwanted = '</div><div class="product-list_row">';
      var comment='<!-- so cool -->';
      newDiv = newDiv.split(unwanted).join(comment);
      document.getElementById('product').innerHTML = newDiv;
      &lt;div class="product-list" id="product"&gt;

      【讨论】:

      • 不幸的是,我不能使用正则表达式,因为这是在 shopify 网站中,并且他们的流动框架不适用于正则表达式。
      • 检查一下,它将替换所有不需要的元素。
      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2021-01-13
      • 2023-01-12
      • 1970-01-01
      相关资源
      最近更新 更多