[英]Function is not defined - Uncaught ReferenceError
我有這個“Uncaught ReferenceError: function is not defined”錯誤不明白。
如果我有
$(document).ready(function() {
function codeAddress() {
var address = document.getElementById("formatedAddress").value;
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
}
});
和
<input type="image" src="btn.png" alt="" onclick="codeAddress()" />
<input type="text" name="formatedAddress" id="formatedAddress" value="" />
當我按下按鈕時,它將返回“Uncaught ReferenceError”。
但是,如果我將codeAddress()
放在$(document).ready(function(){})
之外,那么它工作正常。
我的意圖是將codeAddress()
放在document.ready
function 中。
問題是codeAddress()
沒有足夠的范圍可以從按鈕調用。 您必須在ready()
的回調之外聲明它:
function codeAddress() {
var address = document.getElementById("formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
}
$(document).ready(function(){
// Do stuff here, including _calling_ codeAddress(), but not _defining_ it!
});
如何刪除onclick
屬性並添加一個 ID:
<input type="image" src="btn.png" alt="" id="img-clck" />
還有你的腳本:
$(document).ready(function(){
function codeAddress() {
var address = document.getElementById("formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
}
$("#img-clck").click(codeAddress);
});
這樣,如果您需要更改函數名稱或不需要觸摸 html 的任何內容。
您的問題是您不了解您設置的范圍。
您正在將ready
函數傳遞給函數本身。 在此函數中,您將創建另一個名為codeAddress
函數。 這個存在於創建它的范圍內,而不是在 window 對象中(所有東西和它的叔叔都可以調用它)。
例如:
var myfunction = function(){
var myVar = 12345;
};
console.log(myVar); // 'undefined' - since it is within
// the scope of the function only.
在這里查看更多關於匿名函數的信息: http : //www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
另一件事是我注意到您在該頁面上使用了 jQuery。 這使得設置點擊處理程序變得更加容易,並且您無需在 HTML 中設置“onclick”屬性的麻煩。 您也不需要讓所有的人都可以使用codeAddress
方法:
$(function(){
$("#imgid").click(function(){
var address = $("#formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
});
});
(您應該刪除現有的onclick
並為要處理的圖像元素添加一個 ID)
請注意,我已將$(document).ready()
替換$(document).ready()
$()
( http://api.jquery.com/ready/ ) 的快捷方式。 然后使用click方法為元素分配一個點擊處理程序。 我還用 jQuery 對象替換了你的document.getElementById
。
您必須在 ready() 之外編寫該函數體;
因為 ready 用於調用一個函數或綁定一個具有綁定 id 的函數,就像這樣。
$(document).ready(function() {
$("Some id/class name").click(function(){
alert("i'm in");
});
});
但是如果你在 onchange/onclick 事件中調用“showAmount”函數,你就不能這樣做
$(document).ready(function() {
function showAmount(value){
alert(value);
}
});
你必須在 ready() 之外寫上“showAmount”;
function showAmount(value){
alert(value);//will be called on event it is bind with
}
$(document).ready(function() {
alert("will display on page load")
});
還有一件事。 根據我的經驗,發生此錯誤是因為在Function is not defined - uncaught referenceerror
之前存在另一個錯誤Function is not defined - uncaught referenceerror
。
因此,查看控制台以查看是否存在先前的錯誤,如果存在,請更正任何存在的錯誤。 您可能很幸運,因為它們是問題所在。
清除緩存為我解決了這個問題,或者您可以在其他瀏覽器中打開它
function myFun() {
$('h2').html("H999999");
}
<html>
<head>
<title>Reader</title>
</head>
<body>
<h2>${message}</h2>
<button id="hi" onclick="myFun();" type="submit">Hi</button>
</body>
</html>
請在 function 之外添加 function。准備好 function。
$(document).ready(function () {
listcoming();
});
function listcoming() {}
像這樣...它沒有足夠的空間和 scope inside the.ready f
請檢查您是否正確提供了 js 引用。 首先是 JQuery 文件,然后是您的自定義文件。 由於您在 js 方法中使用了 '$'。
我想我會提到這一點,因為我花了一段時間來解決這個問題,而且我在 SO 上的任何地方都找不到答案。 我正在處理的代碼為同事工作,但不適用於我(我遇到了同樣的錯誤)。 它在 Chrome 中對我有效,但在 Edge 中無效。
我能夠通過清除 Edge 中的緩存來使其工作。
這可能不是這個特定問題的答案,但我想我會提到它,以防它為其他人節省一點時間。
有時是因為緩存和cookies,我們在js文件上創建的函數無法被瀏覽器加載。 因此,請嘗試清除緩存和 cookie 或在同一頁面上按 CTRL+F5。 此外,您可以嘗試在隱身模式下打開它。
只需將 function 移動到外部 js 文件,因為它包含 $ 代碼此錯誤將得到解決
如果將 function 放在 () 之間,則它是本地 function,因此 function 僅用於該腳本。
(function (){ // Local Function });
所以如果你刪除括號,你會得到一個文檔 function。
(function (){ // Document Function });
然后你就可以在每個腳本中使用 function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.