[英]Javascript Uncaught Reference error Function is not defined
[英]Uncaught reference error: function is not defined javascript
我編寫了一個帶有參數的函數,並且試圖調用該函數。 但是,我不斷收到“未捕獲的引用錯誤:”函數”未定義。
這是我的代碼:
var bColor, fColor, fStyle, bUrl;
jQuery(function changeBackground(bColor, fColor, fStyle, bUrl) {
if (typeof (bColor) === 'undefined') bColor = '#fff';
if (typeof (bUrl) === 'undefined') bUrl = '';
if (typeof (fColor) === 'undefined') fColor = '#000';
if (typeof (fStyle) === 'undefined') fStyle = 'Monda';
WebFontConfig = {
google: { families: [ fStyle ] }
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
jQuery('body').css({ backgroundColor: bColor, backgroundUrl: bUrl, color: fColor, fontFamily: fStyle });
});
無論是否從腳本運行函數,我都會收到錯誤消息。 或者,如果我通過任何元素調用(例如onload)運行它。
我不知道為什么要得到錯誤,也許另一雙眼睛可以發現我的錯誤。
當您將函數傳遞給$(fn)
之類的jQuery對象時,它與$(document).ready(fn)
。 您的第一個參數bColor
實際上是jQuery
。 您可能想要的是將所有內容包裝在ready事件中,並在該閉包內使用$
:
jQuery(function($){
// use `$` from now on instead of `jQuery`
var bColor, fColor, fStyle, bUrl;
function changeBackground() {
...
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.