Javascript未捕获引用错误:未定义closeInfoBox(实际定义时)

Javascript未捕获引用错误:未定义closeInfoBox(实际定义时),javascript,jquery,referenceerror,Javascript,Jquery,Referenceerror,我的代码有问题,导致它无法看到我的函数,即使它在那里 这是我正在使用的代码: if (markerNodes.length > 0) { $("#locationInformation").html('<button align="center" class="btn-orange"' + 'style="margin-right: 50px;" id="closeInfo" onclick="closeInfoBox();">' +

我的代码有问题,导致它无法看到我的函数,即使它在那里

这是我正在使用的代码:

if (markerNodes.length > 0) {
     $("#locationInformation").html('<button align="center" class="btn-orange"' +
        'style="margin-right: 50px;" id="closeInfo" onclick="closeInfoBox();">' +
        'SEARCH AGAIN</button>');                
} else {
     alert('Sorry, there are no stores that close to your location.' +
             'Try expanding your search radius.');
}
if(markerNodes.length>0){
$(“#位置信息”).html(“”+
“再次搜索”);
}否则{
警报('抱歉,您所在位置附近没有商店。'+
“尝试扩大搜索半径。”);
}
如您所见,我正在动态地将该按钮添加到html div idlocationInformation

在同一个.JS文件中,我有一个函数,它说它找不到:

(function ($) {
  ....[lots of code here]...

  function searchLocationsNear(center) {
     ....[lots of code here]...

     if (markerNodes.length > 0) {
          $("#locationInformation").html('<button align="center" class="btn-orange"' +
          'style="margin-right: 50px;" id="closeInfo" onclick="closeInfoBox();">' +
          'SEARCH AGAIN</button>');                
     } else {
          alert('Sorry, there are no stores that close to your location.' +
                'Try expanding your search radius.');
     }

     ....[lots of code here]...
  }

  ....[lots of code here]...

  function closeInfoBox() {
      if (hasSearched == true) {
          hasSearched = false;
          $("#addressInput").val('');
          clearLocations();
          $('.radiusDropDown').val('30');
          $('#addressInput').focus();
      }

      if (hasDirections == true) {
          slideDirections("yes");
          hasDirections = false;
      }
  }

  ....[lots of code here]...

}(jQuery));
(函数($){
..[这里有很多代码]。。。
功能搜索位置导航(中){
..[这里有很多代码]。。。
如果(markerNodes.length>0){
$(“#位置信息”).html(“”+
“再次搜索”);
}否则{
警报('抱歉,您所在位置附近没有商店。'+
“尝试扩大搜索半径。”);
}
..[这里有很多代码]。。。
}
..[这里有很多代码]。。。
函数closeInfoBox(){
如果(hasearched==true){
hasearched=假;
$(“#地址输入”).val(“”);
clearLocations();
$('radiudropdown').val('30');
$('#addressInput').focus();
}
if(hasDirections==true){
幻灯片方向(“是”);
hasDirections=假;
}
}
..[这里有很多代码]。。。
}(jQuery));
因此,综上所述,.JS文件如下所示(省略不需要显示的代码):

(函数($){
..[这里有很多代码]。。。
功能搜索位置导航(中){
..[这里有很多代码]。。。
如果(markerNodes.length>0){
$(“#位置信息”).html(“”+
“再次搜索”);
}否则{
警报('抱歉,您所在位置附近没有商店。'+
“尝试扩大搜索半径。”);
}
..[这里有很多代码]。。。
}
..[这里有很多代码]。。。
函数closeInfoBox(){
如果(hasearched==true){
hasearched=假;
$(“#地址输入”).val(“”);
clearLocations();
$('radiudropdown').val('30');
$('#addressInput').focus();
}
if(hasDirections==true){
幻灯片方向(“是”);
hasDirections=假;
}
}
}(jQuery));

您的函数是在
$()
就绪处理程序中定义的,因此不作为全局函数可见

使用内联事件处理程序创建元素确实没有什么好的理由:

      $("#locationInformation")
        .empty()
        .append($("<button/>", {
            "class": "btn-orange",
            "css": { "margin-right": "50px" }, //Needed ":"
            "id": "closeInfo",
            "click": closeInfoBox,
            "text": "SEARCH AGAIN"
        }));

无论按钮被添加和删除多少次,这都将保持有效。

您的函数在
$()
就绪处理程序中定义,因此不作为全局函数可见

使用内联事件处理程序创建元素确实没有什么好的理由:

      $("#locationInformation")
        .empty()
        .append($("<button/>", {
            "class": "btn-orange",
            "css": { "margin-right": "50px" }, //Needed ":"
            "id": "closeInfo",
            "click": closeInfoBox,
            "text": "SEARCH AGAIN"
        }));

无论按钮被添加和删除多少次,这都将保持有效。

您的函数在
$()
就绪处理程序中定义,因此不作为全局函数可见

使用内联事件处理程序创建元素确实没有什么好的理由:

      $("#locationInformation")
        .empty()
        .append($("<button/>", {
            "class": "btn-orange",
            "css": { "margin-right": "50px" }, //Needed ":"
            "id": "closeInfo",
            "click": closeInfoBox,
            "text": "SEARCH AGAIN"
        }));

无论按钮被添加和删除多少次,这都将保持有效。

您的函数在
$()
就绪处理程序中定义,因此不作为全局函数可见

使用内联事件处理程序创建元素确实没有什么好的理由:

      $("#locationInformation")
        .empty()
        .append($("<button/>", {
            "class": "btn-orange",
            "css": { "margin-right": "50px" }, //Needed ":"
            "id": "closeInfo",
            "click": closeInfoBox,
            "text": "SEARCH AGAIN"
        }));

无论按钮被添加和删除多少次,该选项都将保持有效。

closeInfoBox
未在全局范围内定义。内联事件处理程序只能处理全局可见的变量。您可以使用,而不必在全局范围内使用
onclick
函数。“align”不是
元素的有效属性。
closeInfoBox
未在全局范围内定义。内联事件处理程序只能处理全局可见的变量。您可以使用,而不必在全局范围内使用
onclick
函数。“align”不是
元素的有效属性。
closeInfoBox
未在全局范围内定义。内联事件处理程序只能处理全局可见的变量。您可以使用,而不必在全局范围内使用
onclick
函数。“align”不是
元素的有效属性。
closeInfoBox
未在全局范围内定义。内联事件处理程序只能处理全局可见的变量。您可以使用,而不必在全局范围内使用
onclick
函数。“align”不是
元素的有效属性。是否有任何方法引用这样的内部函数?我有一个项目,我使用了一个第三方控件,该控件有一个javascript回调。因为回调必须是全局的,所以当我真的不想时,我必须使我的所有代码都是全局的。@ToniW不,没有,除非它们通过分配给全局符号显式导出。如果它们只是声明为函数,而没有进行任何导出操作,那么它们是周围函数的本地函数,从外部看不到。有没有办法引用这样的内部函数?我有一个项目,我使用了一个第三方控件,该控件有一个javascript回调。因为回调必须是全局的,所以当我真的不想时,我必须使我的所有代码都是全局的。@ToniW不,没有,除非它们通过分配给全局符号显式导出。如果它们只是声明为函数,而没有进行任何导出操作,则它们是周围函数的本地函数,从外部看不到。是否有方法引用内部f