Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript和jquery加载外部字体_Javascript_Jquery_Html_Fonts - Fatal编程技术网

使用javascript和jquery加载外部字体

使用javascript和jquery加载外部字体,javascript,jquery,html,fonts,Javascript,Jquery,Html,Fonts,我有一个用jquery动态创建的元素,我想用硬盘上的自定义字体来设置它的样式 你能给我一个方法来加载那个字体并应用到这个元素上吗 现在,该对象看起来像: $("<p>").css({ fontFamily : "Arial", fontSize: "13px", color: "#000"}); $(“”).css({fontFamily:“Arial”, 字体大小:“13px”, 颜色:#000“}); 我想用我的自定

我有一个用jquery动态创建的元素,我想用硬盘上的自定义字体来设置它的样式

你能给我一个方法来加载那个字体并应用到这个元素上吗

现在,该对象看起来像:

$("<p>").css({ fontFamily : "Arial",
               fontSize: "13px",
               color: "#000"});
$(“”).css({fontFamily:“Arial”,
字体大小:“13px”,
颜色:#000“});
我想用我的自定义字体替换fontFamily:“Arial”

谢谢,
Gabriel

您需要在样式表中添加一些CSS-您可以在此处找到要添加的正确CSS:

然后,您可以使用任何名称更改字体名称,例如:

$("<p>").css({ fontFamily : "Kimberley",
               fontSize: "13px",
               color: "#000"});
$(“”).css({fontFamily:“金伯利”,
字体大小:“13px”,
颜色:#000“});
EDIT:更好的方法是在样式表中为标记添加一个类,然后使用jquery将该类添加到p中

p.example{ font-family:Arial, Helvetica, sans-serif; }

$("<p>").addClass("example");
p.example{font-family:Arial、Helvetica、sans-serif;}
$(“”).addClass(“示例”);

我个人使用cufon

适用于99%的浏览器


享受吧

我在jquery中使用fontface插件找到了一个很好的解决方案。
这段代码完成了所有工作:

$("head").prepend("<style type=\"text/css\">" + 
                                "@font-face {\n" +
                                    "\tfont-family: \"myFont\";\n" + 
                                    "\tsrc: local('☺'), url('myFont.otf') format('opentype');\n" + 
                                "}\n" + 
                                    "\tp.myClass {\n" + 
                                    "\tfont-family: myFont !important;\n" + 
                                "}\n" + 
                            "</style>");
$(“head”).prepend(“+
“@font-face{\n”+
“\t字体系列:\“myFont\”;\n”+
“\tsrc:local('☺'), url('myFont.otf')格式('opentype');\n“+
“}\n”+
“\tp.myClass{\n”+
“\t字体系列:myFont!重要;\n”+
“}\n”+
"");

将没有样式表。所有内容都将在这个.js文件中。这不是他所问的问题的答案。这是一个简化版本。根据需要修改。
$(“head”).prepend(@font-face{font-family:'customFont';src:url('file://“+fontfile+”);
$(“#url”).css(“字体系列”,“自定义字体”)