Load external font with javascript and jquery

前端 未结 4 1751
予麋鹿
予麋鹿 2020-12-15 12:33

I have a < p > element created dynamically with jquery, and I want to style it with a custom font that I have on my hard drive.

Could you sugest me a method to lo

相关标签:
4条回答
  • 2020-12-15 12:38

    You will need to add some CSS to your style sheet - You can find the right CSSS to add here: How do I load external fonts into an HTML document?

    You can then change the font name with whatever its called eg:

    $("<p>").css({ fontFamily : "Kimberley",
                   fontSize: "13px",
                   color: "#000"});
    

    EDIT: What would be better is to add a class in your stylesheet for the

    tag and then add that class to to your p with jquery

    p.example{ font-family:Arial, Helvetica, sans-serif; }
    
    $("<p>").addClass("example");
    
    0 讨论(0)
  • 2020-12-15 12:54

    Personally I use cufon.

    Works on 99% of browsers out there!

    http://cufon.shoqolate.com/generate/

    Enjoy!

    0 讨论(0)
  • 2020-12-15 12:58

    This piece of code does all the job:

    $("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>");
    
    0 讨论(0)
  • 2020-12-15 13:00

    I found a good solution using fontface plugin for jquery. http://www.sitepoint.com/the-fontface-jquery-plugin/

    0 讨论(0)
提交回复
热议问题