【发布时间】:2019-02-15 23:59:55
【问题描述】:
我正在尝试在 HTML 中的公司名称旁边添加 商标符号 TM。
使用下面的文字:
<sup>TM</sup>
或
™
我有一个渲染的 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™"));
});
上面的选项替换了“HTML标签属性的值”,即alt,img的标题,这是我不想要的。
$("body").children().each(function () {
$(this).html($(this).text().replace(/Walmart Inc/g, "Walmart Inc™"));
});
& 这会导致从正文中删除所有 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'; }
-
因为有上百页,无法到处添加
<span class='tradeMark'>Walmart Inc</span>。 -
你试过这个吗:stackoverflow.com/questions/25109275/…???
-
在您的第二个解决方案变体中,您使用
$.html()设置每个元素的html,但您设置的值也取自$.text()。我相信$.text()正在剥离标签并将它们视为纯文本。 -
@FreemanLambda,也试过了,它会抛出文本,从而完全消除 HTML 并仅提供文本输出
标签: javascript jquery html