我的cloneFolderHTML没有定义

时间:2017-12-21 22:59:03

标签: javascript jquery

我收到这样的错误。

  

ReferenceError:未定义cloneFolderHTML

    initialCopy.push({
        clonefolder: cloneFolderHTML
      });

这使我无法访问元素,我不得不重新加载页面,以便代码可以工作。 我只知道打开控制台时它会变为cloneFolderHTML未定义。

如何在submitButton函数之外调用它?

jQuery:

$('.submitButton').click(function() {
  function copyFolderList(cloneFoldername) {
    var cloneFolder = /** HTMLCODE **/;
    return cloneFolder;
  }

  if ( /* code */ ) {
    // some code
  } else {
    var cloneFolder = JSON.parse(localStorage.getItem("copyList"));
    if (cloneFolder == null) {
      cloneFolder = [];
    }
    let cloneFolderHTML = copyFolderList(inputan);
    cloneFolder.push({
      clonefolder: cloneFolderHTML
    });
    localStorage.setItem("copyList", JSON.stringify(cloneFolder));
    $("#navcopyrow").append(cloneFolderHTML);
  }
}); // submitButton function


var cloneFolder = JSON.parse(localStorage.getItem("copyList"));
if (cloneFolder != null) {
  //get the nav reference in DOM
  let navcopy = $("#navcopyrow");

  //clear the html contents
  navcopy.html('');

  for (var i = 0; i < cloneFolder.length; i++) {
    var copyitem = cloneFolder[i];
    $("#navcopyrow").append(copyitem.clonefolder);
  }
} else {
  let initialCopy = [];
  initialCopy.push({
    clonefolder: cloneFolderHTML
  });
  localStorage.setItem("copyList", JSON.stringify(initialCopy));
}

我可以在cloneFolderHTML功能之外拨打submitButton吗?

1 个答案:

答案 0 :(得分:0)

let cloneFolderHTML

let是一个ECMAScript 6保留字,它使变量块作用域。这意味着它只会存在于它的范围内,它会立即被封装{}。由于变量位于else的{}中,因此它只存在于else块内。

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let