【问题标题】:Replace old value with new value excluding the children用新值替换旧值,不包括子项
【发布时间】:2020-08-03 10:38:41
【问题描述】:

A、B、C、D的首字母和数字需要在前端去掉,因为我在后端需要。

表格行的HTML结构是这样的:

<tr ng-repeat="(key, field) in nbd_fields" ng-show="field.enable &amp;&amp; field.published" class="ng-scope">
  <td class="ng-binding">A,B,C,D: 1 - Auswahl Wunschkarte : <b class="ng-binding">Wähle eine Option</b>
  </td>
  <td ng-bind-html="field.price | to_trusted" class="ng-binding"></td>
</tr>

输入前:

电流输出:

如果您注意到所选选项也不可见。是因为 $(window).load() 吗?

所需输出:

我正在使用的代码:

jQuery(".ng-scope td.ng-binding:first-child").text(function(i, oldVal) {
    return oldVal.replace(/^[^-]+ - /,""); 
 });
});

怎样才能让它不影响里面的&lt;b&gt;标签?

我将上述代码用于同一页面上带有不同选择器的步骤标题*,它之所以有效,是因为它没有任何要更改的子项。

我必须将它包裹在 $(window).load() 周围,以便在加载表格后应用更改。 $(document).ready() 没有使用它。不知道为什么?

【问题讨论】:

    标签: javascript jquery angularjs regex wordpress


    【解决方案1】:

    (编辑:修改以适应下面评论中重述的要求。)

    从第一列表格单元格的文本中删除“直到并包括'-'”的所有内容,而其余部分保持不变:

    // strip "everything up to and including the '-'"
    // from table cell contents
    function stripPrefix(tblCell) {
     // only evaluate first td in tr
      if (tblCell.previousElementSibling) {
        return;
      }
      const tNode = tblCell.firstChild;
      // ignore if table cell is empty
      if (!tNode) {
        return;
      }
      const chars = tNode.nodeValue.split('');
      const iFirstDash = chars.indexOf('-');
      if (iFirstDash === -1) { return; }
      tNode.nodeValue = chars.slice(iFirstDash+1).join('');
    }
    
    function stripAllPrefixes() {
      const tds = document.getElementsByTagName('td');
      for (const td of tds) {
        stripPrefix(td);
      }
    }
    td {
      border: 1px solid gray;
    }
    <h4>Strip "everything up to and including the '-'" from Table Cells</h4>
    <table>
      <tr>
        <td>A,B,C,D: 1 - Auswahl Wunschkarte : <b>Wähle eine Option</b></td>
        <td></td>
      </tr>
      <tr>
        <td>B,C,D,E: 20 - A different leader : <b>should also be stripped</b></td>
        <td></td>
      </tr>
      <tr>
        <td>Oops no dash here <b>Just checking</b></td>
        <td></td>
      </tr>
    
    </table>
    
    <button onclick="stripAllPrefixes();">Strip All</button>

    【讨论】:

    • 感谢您的意见。但我不能通过函数传递值。如果你看到演示页面,你就会有一个想法。可以修改给定的jQuery代码吗?
    • 我想你还是没看懂这个问题。如果您检查 jQuery 代码中的正则表达式,它会删除所有内容,包括 -。我不提供“A、B、C、D”之类的东西。它只是一个参考,它可以是任何东西。想再读一遍吗? :)
    • 我错过了要求剥离“包括 - 在内的所有内容”的部分。我将更新代码以适应这一新要求。
    • 谢谢特里,它成功了。我很感激您使用 vanilla JS 来回答这个问题。出于某种原因,TagName 不起作用,ClassName 为我工作。我还必须包含 window.onload = function() 替换按钮代码。
    【解决方案2】:

    它不会影响b 标签,您的代码正在运行,您只需要使用正确的方法并替换 HTML 代码而不是文本节点:

    jQuery(".nbd-field-header label, .nbo-summary-table .ng-binding").html(function(i, oldVal) {
        return oldVal.replace(/^[^-]+ - /,"");
    });
    

    【讨论】:

    • 谢谢,但是 window.load() 呢。有更好的处理程序吗?这样我在页面加载期间看不到文本更改。
    • 但是现在 b 标签现在在每个 td 上给出一个输出“Wahle eine option”。这段代码是如何操纵它的?
    • 同样使用该代码也会影响同一行中的其他元素。所以这就是为什么我不得不使用:first-child。
    • 这只是问题所在的一个示例,您仍然可以更改选择器。对于 window.load,我认为您的元素稍后会插入到 DOM 中,而不是在加载期间。
    猜你喜欢
    • 2010-12-10
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2021-06-09
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多