简体   繁体   English

用jQuery忽略HTML标签中的数字

[英]Ignore numbers in html tags with jquery

I have a webfont I really want to use, however the font designer got lazy when creating the number characters. 我有一个我真的想使用的webfont,但是字体设计器在创建数字字符时变得很懒。 They are all different sizes compared to letter characters. 与字母字符相比,它们的大小都不同。

I'm trying to standardize them by applying a unique bit of css to each number character. 我试图通过对每个数字字符使用唯一的CSS来标准化它们。 This is only going to apply to h1 tags. 这仅适用于h1标签。

My technique in jquery is to search through text and apply a <span class="font-fix-1">1</span> for the number 1, <span class="font-fix-2">2</span> for the number 2 etc. 我在jquery中的技术是搜索文本并为数字1加上<span class="font-fix-1">1</span><span class="font-fix-2">2</span>表示2等

However, I don't want to target numbers which may be in html scripts. 但是,我不想定位可能在html脚本中出现的数字。 For example, if I have the following h1: 例如,如果我具有以下h1:

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

I don't want the script to replace the number "2" in the <sup> tag, just the <h1> tag only. 我不希望脚本替换<sup>标记中的数字“ 2”,仅替换<h1>标记。

Does anyone know how to recognize this with javascript? 有谁知道如何用javascript识别这一点?

My script I have so far is: 到目前为止,我的脚本是:

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);
});

You can do like this 你可以这样

HTML 的HTML

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

JavaScript 的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