【问题标题】:Ignore numbers in html tags with jquery使用jquery忽略html标签中的数字
【发布时间】:2014-03-25 06:36:13
【问题描述】:

我有一个我非常想使用的网络字体,但是字体设计师在创建数字字符时变得懒惰了。与字母字符相比,它们的大小都不同。

我试图通过对每个数字字符应用独特的 css 位来标准化它们。这仅适用于 h1 标签。

我在 jquery 中的技术是搜索文本并为数字 1 应用 <span class="font-fix-1">1</span>,为数字 2 应用 <span class="font-fix-2">2</span> 等等。

但是,我不想定位可能在 html 脚本中的数字。 例如,如果我有以下 h1:

<h1>This is a Heading with the number 20 in it and some <sup style="font-size:12px">annoying html</sup></h1>

我不希望脚本替换 &lt;sup&gt; 标记中的数字“2”,只替换 &lt;h1&gt; 标记。

有谁知道如何用 javascript 识别这个?

到目前为止,我的脚本是:

var h1_text = "";
jQuery("h1").each(function(){
    h1_text = jQuery(this).html();
    h1_text = h1_text.replace(/1/g, '<span class="font-fix-1">1</span>');
    jQuery(this).html(h1_text);
});

【问题讨论】:

  • 数字字形的大小是字体设计师的决定,通常因字体设计而异。如果您不喜欢这个决定,请使用其他字体。
  • ioseph,我检查了这个,它与我正在寻找的功能不同。我需要能够编辑 node===3 文本,然后将其添加回包含原始标签的原始字符串。

标签: javascript jquery html css regex


【解决方案1】:

你可以这样做

HTML

<span>
    <a>123456789</a> //number that you want to ignore
    Text I want //the text that you want
</span>

JavaScript

alert($("span").contents().filter(function(){
    return this.nodeType === 3;
}).text())

【讨论】:

  • 这并不能完全解决我的问题。它允许我查看不在标签中的文本,但是我不知道如何编辑此文本并将其注入字符串。
猜你喜欢
  • 2013-05-16
  • 2012-01-01
  • 2013-02-15
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
相关资源
最近更新 更多