2015-11-24 50 views
0

GoogleWeb字體的定義 - 使用字體的字體變量的jQuery

var fonts = []; 
 
    $.get("https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyBJ9zGTIOTxFHbAwdPJG9cAXmsP-h3mhzY", callbackFuncWithData) 
 
    function callbackFuncWithData(data) 
 
     { 
 
     $.each(data.items, function (index, value) { 
 
      fonts += value.family; 
 
     }); 
 

 
     }

我嘗試不同的方法以達到填充「字體」變量,但它不會工作。

console.log(fonts, "my var");

回答

0

這不是你如何將值插入您的數組:

var fonts = []; 
fonts += value.family; 

您需要使用.push方法:

var fonts = []; 
 

 
$.get("https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyBJ9zGTIOTxFHbAwdPJG9cAXmsP-h3mhzY", callbackFuncWithData); 
 

 
function callbackFuncWithData(data) 
 
{ 
 
    $.each(data.items, function (index, value) { 
 
    fonts.push(value.family); // <-- that's how you insert values :) 
 
    }); 
 

 
    // Just an example output. Don't copy it to your code: 
 
    document.body.innerText = JSON.stringify(fonts); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>