繁体   English   中英

用jQuery忽略HTML标签中的数字

[英]Ignore numbers in html tags with jquery

我有一个我真的想使用的webfont,但是字体设计器在创建数字字符时变得很懒。 与字母字符相比,它们的大小都不同。

我试图通过对每个数字字符使用唯一的CSS来标准化它们。 这仅适用于h1标签。

我在jquery中的技术是搜索文本并为数字1加上<span class="font-fix-1">1</span><span class="font-fix-2">2</span>表示2等

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

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

我不希望脚本替换<sup>标记中的数字“ 2”,仅替换<h1>标记。

有谁知道如何用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);
});

你可以这样

的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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM