使所有带有 Helvetica 字体的文本在 jQuery 中都具有 Arial 字体

发布于 2024-07-30 10:38:26 字数 81 浏览 3 评论 0原文

我需要一些 jQuery 魔法来查找页面上具有给定字体(例如 Helvetica)的所有文本,并仅将该文本的字体更改为其他字体(例如 Arial)。

I need some jQuery magic that will look for all text on a page with a given font (say Helvetica), and change the font of only that text to something else (say Arial).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一身软味 2024-08-06 10:38:26

这应该可行,我在我的网站上检查了它并且它正确地更改了字体。

$('body').filter(function() {
    return ( $(this).css('font-family').indexOf('Helvetica')>-1);
}).css('font-family', 'Arial');

This should work, I checked it on my site and it changed font properly.

$('body').filter(function() {
    return ( $(this).css('font-family').indexOf('Helvetica')>-1);
}).css('font-family', 'Arial');
囍孤女 2024-08-06 10:38:26

这并不是真正的答案,但由于我还无法对其他人的答案发表评论,因此我将我的评论放在答案中。

思考者的上述答案将不起作用(或者至少在某些情况下它不起作用)。

Thinker 的代码如下(我将其复制到此处,以防他可以编辑他的答案):

$('body').filter(function() {
return ( $(this).css('font-family').indexOf('Helvetica')>-1);
}).css('font-family', 'Arial');

这段代码的作用是将包含任何字体系列的字体更改为 Arial em> Helvetica。 其中包括 Helvetica 用作后备的字体系列,因此很可能并不总是显示。

您还可以阅读以下帖子中的答案和评论:

Changing Body Font-Size based on Font-Family with jQuery

其中包含类似的代码和讨论。

This is not really an answer, but since I cannot yet comment on other people's answers, I'm placing my comments here in an answer.

Thinker's answer above will not work (or at least there are cases where it will not work).

Thinker's code is as follows (I'm copying it here in case he can edit his answer):

$('body').filter(function() {
return ( $(this).css('font-family').indexOf('Helvetica')>-1);
}).css('font-family', 'Arial');

What this code will do is change the font to Arial for any font family which contains Helvetica. That includes font families where Helvetica is being used as a fallback, and so very probably is not always displayed.

You can also read the answers and comments in the following post:

Changing Body Font-Size based on Font-Family with jQuery

which contains similar code along with a discussion.

谁与争疯 2024-08-06 10:38:26

如果一切都是通过 CSS 定义的,您是否想过生成自己的 @font-face 定义,覆盖 Helvetica?

例如,如果所有内容都定义为在 Helvetica 中呈现,如以下语句所示:

* {

    font-family: Helvetica;

}

然后,您可以将 @font-face 语句附加到样式表,现在看起来像这样:

@font-face {

    font-family: 'Helvetica';

    src:    local('Anivers-Regular'),
        local('Anivers Regular'),
        url('typeface.Anivers.Regular.otf') format('truetype');

}

它适用于内联样式元素,例如 < code>

aw

也是如此。

这样,Helvetica 的所有内容都会自动在(例如)Anivers 中呈现,并且此代码片段可以使用或不使用双引号。 列出 PostScript 名称、全名 + 样式,然后列出跨浏览器兼容性的 URI。 如果后备是 Helvetica,这个 CSS 魔法就会起作用。 另外,您可以通过这种方式获得免费的性能开销,因为浏览器(而不是您的脚本)会为您进行替换。

不幸的是,Internet Explorer 无缘无故地讨厌 .otf 文件,您需要一个名为 ttf2eot 的实用程序以防万一。

这是一个丑陋的 hack,并且会在你的生产代码中存在太久。

顺便问一下,场景是什么?您为什么要这样做——用较差的字体替换更好看的字体?

如果您后来改变了主意并想在其他地方使用“真正的”Helvetica,则必须将其更改为另一个名称。 繁琐的工作不是吗。

If everything were defined via CSS, have you ever thought of spawning your own @font-face definitions, overriding Helvetica?

For example, if everything is defined to render in Helvetica, like in this statement:

* {

    font-family: Helvetica;

}

You may then append a @font-face statement to your style sheet, which would now look like this:

@font-face {

    font-family: 'Helvetica';

    src:    local('Anivers-Regular'),
        local('Anivers Regular'),
        url('typeface.Anivers.Regular.otf') format('truetype');

}

It works on in-line-styled elements like <h4 style="font-family: Helvetica;">aw</h4> too.

In that way, everything Helvetica would be automagically rendered in (, e.g.,) Anivers, and this snippet works with or without double quotes. List PostScript name, Full name + style, then an URI for cross-browser compatibility. If the fallback is on Helvetica, this CSS magic would work. Plus, you get free performance overhead in this way since the browser, instead of your script, does the substitution for you.

Unfortunately, Internet Explorer hates .otf files for no apparent reason and you would need an utility called ttf2eot just in case.

This is an ugly hack and shall live in your production code for too long.

By the way, what’s the scenario and why would you want to do this — substituting a better-looking typeface with an inferior one?

If you later changed your mind and would like to use the “real” Helvetica somewhere else, you will have to swizzle it under another name. Tedious work isn’t it.

孤星 2024-08-06 10:38:26

这是我在 Google Chrome 扩展程序中使用的内容:

var replacement = "Helvetica";
var toReplace = new RegExp("[\\'\\\"\\s]*(Arial|Some|Other|Fonts)[\\'\\\"\\s]*(?=,|$)", "gi");

function replaceFont (toReplace, replacement) {
    // Adjust style sheets
    if (document.styleSheets) {
        for (var i = 0; i < document.styleSheets.length; i++) {
            var styleSheet = document.styleSheets[i];

            if (styleSheet.cssRules) {
                for (var j = 0; j < styleSheet.cssRules.length; j++) {
                    var cssRule = styleSheet.cssRules[j];

                    // Replace in font-family attribute
                    if (cssRule.style.fontFamily.match(toReplace))
                        cssRule.style.fontFamily = cssRule.style.fontFamily.replace(toReplace, replacement);

                    // Replace in font attribute
                    if (cssRule.style.font.match(toReplace))
                        cssRule.style.font = cssRule.style.font.replace(toReplace, replacement);
                }
            }
        }
    }

    // Adjust inline styles
    $('body').find('*').each(
        function (i) {
            if ($(this).css('font-family').match(toReplace))
                $(this).css('font-family', $(this).css('font-family').replace(toReplace, replacement));

            if ($(this).css('font').match(toReplace))
                $(this).css('font', $(this).css('font').replace(toReplace, replacement));
        }
    );

    // Adjust font tag properties
    $('body').find('font').each(
        function (i) {
            if ($(this).attr('face').match(toReplace))
                $(this).attr('face', $(this).attr('face').replace(toReplace, replacement));
        }
    );

}

完整代码位于此处

This is what I use in my Google Chrome extension:

var replacement = "Helvetica";
var toReplace = new RegExp("[\\'\\\"\\s]*(Arial|Some|Other|Fonts)[\\'\\\"\\s]*(?=,|$)", "gi");

function replaceFont (toReplace, replacement) {
    // Adjust style sheets
    if (document.styleSheets) {
        for (var i = 0; i < document.styleSheets.length; i++) {
            var styleSheet = document.styleSheets[i];

            if (styleSheet.cssRules) {
                for (var j = 0; j < styleSheet.cssRules.length; j++) {
                    var cssRule = styleSheet.cssRules[j];

                    // Replace in font-family attribute
                    if (cssRule.style.fontFamily.match(toReplace))
                        cssRule.style.fontFamily = cssRule.style.fontFamily.replace(toReplace, replacement);

                    // Replace in font attribute
                    if (cssRule.style.font.match(toReplace))
                        cssRule.style.font = cssRule.style.font.replace(toReplace, replacement);
                }
            }
        }
    }

    // Adjust inline styles
    $('body').find('*').each(
        function (i) {
            if ($(this).css('font-family').match(toReplace))
                $(this).css('font-family', $(this).css('font-family').replace(toReplace, replacement));

            if ($(this).css('font').match(toReplace))
                $(this).css('font', $(this).css('font').replace(toReplace, replacement));
        }
    );

    // Adjust font tag properties
    $('body').find('font').each(
        function (i) {
            if ($(this).attr('face').match(toReplace))
                $(this).attr('face', $(this).attr('face').replace(toReplace, replacement));
        }
    );

}

Full code is here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文