【问题标题】:Jquery String Format ReplacementJquery 字符串格式替换
【发布时间】:2015-11-14 19:38:27
【问题描述】:

在整个网站(不使用 spans/divs/id 标签)中,如何修改以下代码,以便编写所有 H2O 实例,以便 2 以较小的字体编写。此代码当前修改了整个页面上数字 2 的所有实例——我只想搜索和替换整个站点中用 H2O 编写的 2。

$('body :not(script)').contents().filter(function() {
    return this.nodeType === 3;
}).replaceWith(function() {
    return this.nodeValue.replace(/[2]/g, '<font size="-1">2</font>');
});

【问题讨论】:

  • .replace(/H2O/g, 'H&lt;font size="-1"&gt;2&lt;/font&gt;O'); 工作吗?
  • 改用这个标记 H2O
  • 以上代码有效,谢谢!一皱...当原文在其他标签中时,例如H1,2实际上很小。因此,我需要 font-size 始终为 0.8em,但设置 font size="0.8em" 不起作用。这里的正确语法是什么,总是使 2 略小于父级的文本大小?
  • 回答了我自己的问题:应该是nodeValue.replace(/X2O/g, 'X&lt;span style="font-size:0.8em"&gt;2&lt;/span&gt;O');

标签: jquery font-size


【解决方案1】:

尝试使用 .html(function(index, html){})String.prototype.replace()RegExp /H20/

$("body *:not(script)").html(function(_, html) {
  return html.replace(/H20/, "H<font size=-1>2</font>0")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>H20 abc</div>
<span>def H20 h20 2</span>

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多