2015-07-20 103 views
0

我知道还有很多这种类型的其他问题,但他们都没有给我我的答案。这没有发生错误,我看不到任何错误或语法错误。jquery .css()不能使用字体大小

我试图做一个谷歌的字体选择器(种)使用jQuery:
JS:

$("#submit").click(function() { 
    var fontFamilyOrig = $("#font").val(); 
    var prefix = "//fonts.googleapis.com/css?family="; 
    var fontFamily = fontFamilyOrig.split(' ').join('+'); 
    fontFamily = prefix + fontFamily; 
    var fontSize = $("#font-size").val(); 
    var displayText = $("#display-text").val(); 
    var boldToggle = $("#bold"); 
    var itToggle = $("#italic"); 
    $("#font-link").attr('href', fontFamily); 
    $("#display").css({ 
     'font-family': fontFamilyOrig, 
     'font-size': fontSize 
    }).text(displayText); 
}); 

HTML:

<div> 
      <table><tr> <td>Type the name of the font (in full):</td> <td><input type="text" name="font" id="font"></td></tr> 
     <tr><td> Type your display text:</td> <td><input type="text" id="display-text"></td></tr> 
     <tr> 
      <td>Choose a font size:</td><td><input type="number" value="" id="font-size"></td></tr> 
      <tr> 
       <td>Other options:</td> 
       <td> 
        <label><input type="checkbox" id="bold">Bold</label> 
        <label><input type="checkbox" id="italic">Italic</label> 
       </td> 
      </tr> 
      <tr> 
       <td><input type="submit" value="Go" id="submit"></td> 
      </tr> 

     </table> 
     </div> 
     <div id="display"> 

     </div> 
     <link id="font-link" rel="stylesheet" href=""> 

那么这样做是显示文本的用户以用户指定的字体和字体大小输入。但字体的作品,而字体大小没有。为什么只有字体能够工作,而不是字体大小? FIDDLE

回答

4

需要在字体大小的末尾有px或em是我的猜测。就像你说

fontSize: fontSize + "px"; 

fontSize: fontSize + "em"; 
+0

已经很接近了,我要补充它就像这样:'“字体大小”:(fontSize的+“像素”)' –

+0

谢谢你,可以”还没有给它支票。 – TricksfortheWeb

+0

@TimLewis,我刚做了一个变量,其值为'px',语法为:'.css('font-size',fontSize + otherVariable);' – TricksfortheWeb