【问题标题】:How to replace Text in html excluding tag properties?如何替换 html 中的文本(不包括标签属性)?
【发布时间】:2019-02-15 23:59:55
【问题描述】:

我正在尝试在 HTML 中的公司名称旁边添加 商标符号 TM

使用下面的文字:

<sup>TM</sup>

&trade;

我有一个渲染的 HTML,我必须在其中添加商标符号。

HTML:

注意:我已经提到了 HTML 正文的一小部分

<div class="col-md-3">
<div class="logo"><img src="/wp-content/uploads/2017/02/logo.png" 
onClick="window.location='https://www.walmart.com'" 
alt="Walmart Inc"  title="This is Walmart Inc for your help" style="pointer-events:all" />
</div></div>

<div class="submenu">
    <a href="/blog" target="_blank">Walmart Inc Blog</a>
    <a href="/blog" target="_blank">New Walmart Inc Products</a>
</div>
<meta itemprop="brand" content=" Walmart Inc"><p>Welcome to Walmart Inc.</p>

尝试了以下选项:

$("body").children().each(function () {
    $(this).html($(this).html().replace(/Walmart Inc/g, "Walmart Inc&trade;"));
});

上面的选项替换了“HTML标签属性的值”,即alt,img的标题,这是我不想要的。

$("body").children().each(function () {
    $(this).html($(this).text().replace(/Walmart Inc/g, "Walmart Inc&trade;"));
});

& 这会导致从正文中删除所有 HTML 标记。

如何在替换时排除标签属性内的文本?

期望的输出:

<div class="col-md-3">
<div class="logo"><img src="/wp-content/uploads/2017/02/logo.png" 
onClick="window.location='https://www.walmart.com'" 
alt="Walmart Inc"  title="This is Walmart Inc for your help" style="pointer-events:all" />
</div></div>

<div class="submenu">
    <a href="/blog" target="_blank">Walmart Inc™ Blog</a>
    <a href="/blog" target="_blank">New Walmart Inc™ Products</a>
</div>
<meta itemprop="brand" content=" Walmart Inc"><p>Welcome to Walmart Inc™.</p>

【问题讨论】:

  • 您可以为此使用 css 解决方案。在 HTML 代码中添加 Walmart Inc。在 css 中这样做: .tradeMark::after { content: '&trade'; }
  • 因为有上百页,无法到处添加&lt;span class='tradeMark'&gt;Walmart Inc&lt;/span&gt;
  • 在您的第二个解决方案变体中,您使用$.html() 设置每个元素的html,但您设置的值也取自$.text()。我相信$.text() 正在剥离标签并将它们视为纯文本。
  • @FreemanLambda,也试过了,它会抛出文本,从而完全消除 HTML 并仅提供文本输出

标签: javascript jquery html


【解决方案1】:

仅针对文本节点并仅用更新的 html 替换特定文本节点的方法。不会影响alt, title etc等属性

这将优于替换可能导致丢失事件侦听器的较大 html 块,因为您只替换文本而没有该文本的父元素

var term = "Walmart Inc";

$('body *:contains("' + term + '")')
  .not(':has(:contains("' + term + '"))')
  .contents().each(function() {
    // nodeType for text nodes is 3
    if (this.nodeType === 3 && this.textContent.includes(term)) {
      var html = this.textContent.replace(term, term + '&trade;')
      // replace text node with html
      $(this).replaceWith(html)
    }

  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-3">
  <div class="logo"><img src="/wp-content/uploads/2017/02/logo.png" onClick="window.location='https://www.walmart.com'" alt="Walmart Inc" title="This is Walmart Inc for your help" style="pointer-events:all" />
  </div>
</div>

<div class="submenu">
  <a href="/blog" target="_blank">Walmart Inc Blog</a>
  <a href="/blog" target="_blank">New Walmart Inc Products</a>
</div>
<meta itemprop="brand" content=" Walmart Inc">
<p>Welcome to Walmart Inc.</p>

【讨论】:

  • 您的代码按预期工作。它不包括 html 标签。
  • 唯一可能不起作用的地方是具有独立文本的父级也有一个子级。不确定这是否会发生。喜欢&lt;div&gt;Walmart &lt;p&gt;Walmart&lt;/p&gt;&lt;/div&gt;
  • 知道了。目前,我的 HTML 中没有这种情况,所以总体上它按预期工作
  • 如果是我,我会考虑在文件中进行文本替换。可以使用正则表达式查找仅在标签内的实例
  • 好的..可以将其转换为replace()内的正则表达式,并为倍数添加g标志
【解决方案2】:
function insertTMAfter(name) {
    document.querySelectorAll('*').forEach(function(item){
        if(item.children.length == 0)
            item.innerHTML = item.innerHTML.replace(name, name + "&trade;");
    })
}

insertTMAfter("Stack Overflow");

// or in your case
insertTMAfter("Walmart Inc");

【讨论】:

  • &amp;trade; 是一个 html 实体,如果您使用 innerText,则不会显示为 html。
  • 现在就试试吧!谢谢@Vikrant
  • 更新后的代码按预期工作正常,而且是最少的代码。但它不会替换 Div's 中的文本
猜你喜欢
  • 2013-05-01
  • 2011-03-13
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多