使所有带有 Helvetica 字体的文本在 jQuery 中都具有 Arial 字体
我需要一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可行,我在我的网站上检查了它并且它正确地更改了字体。
This should work, I checked it on my site and it changed font properly.
这并不是真正的答案,但由于我还无法对其他人的答案发表评论,因此我将我的评论放在答案中。
思考者的上述答案将不起作用(或者至少在某些情况下它不起作用)。
Thinker 的代码如下(我将其复制到此处,以防他可以编辑他的答案):
这段代码的作用是将包含任何字体系列的字体更改为 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):
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.
如果一切都是通过 CSS 定义的,您是否想过生成自己的
@font-face
定义,覆盖 Helvetica?例如,如果所有内容都定义为在 Helvetica 中呈现,如以下语句所示:
然后,您可以将 @font-face 语句附加到样式表,现在看起来像这样:
它适用于内联样式元素,例如 < 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:
You may then append a @font-face statement to your style sheet, which would now look like this:
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.
这是我在 Google Chrome 扩展程序中使用的内容:
完整代码位于此处 。
This is what I use in my Google Chrome extension:
Full code is here.