简体   繁体   English

未捕获的 ReferenceError:showCurrentPage 未在 HTMLButtonElement.onclick 中定义

[英]Uncaught ReferenceError: showCurrentPage is not defined at HTMLButtonElement.onclick

Having a mare with this one...有一匹母马与这...

I have a function, which displays pagination ( 1, 2, 3 etc) buttons, for each page of my todo app results.我有一个功能,它为我的待办事项应用结果的每一页显示分页(1、2、3 等)按钮。

Specifically for example, if you click on button 2, you'll see page 2 of the results.具体来说,例如,如果您单击按钮 2,您将看到结果的第 2 页。 Here's the full function and the buttons are being inserted via template literals:这是完整的功能,按钮是通过模板文字插入的:

// CREATE BUTTONS FOR EACH PAGE THAT EXISTS
function displayNumberedButtons(bookMarksArray) {
    for (let x = 0; x < bookMarksArray.length; x++)
    listArray.push(x);

    numberOfPages = Math.ceil(listArray.length / numberPerPage);

    let individualPagesArray = [];
    for (var i = 1; i <= numberOfPages; i++) {
        individualPagesArray.push(i);
    }

    // BUTTONS ARE ADDED HERE
    for (var i = 0; i < individualPagesArray.length; i++) {
        document.getElementById("numbers").innerHTML += `<button onclick=showCurrentPage(${i+1})>` + individualPagesArray[i] + `</button>`;
    }
}

However, my onclick function, does not seem to register in my JavaScript:但是,我的 onclick 函数似乎没有在我的 JavaScript 中注册:

// PAGINGATION CTAS
window.showCurrentPage = (i) => {
    currentPage = i;
    paginationCountLogic(bookMarksArray);
}

If I click on any button, I get the following error.如果我单击任何按钮,则会出现以下错误。 And I have no idea why, as I can see the buttons in my DOM.我不知道为什么,因为我可以看到我的 DOM 中的按钮。

index.html:1 Uncaught ReferenceError: showCurrentPage is not defined at HTMLButtonElement.onclick (index.html:1) index.html:1 Uncaught ReferenceError: showCurrentPage 未在 HTMLButtonElement.onclick (index.html:1) 中定义

This only happens when I compiled my JS files in advanced mode, using google closure compiler.这只发生在我使用 google 闭包编译器以高级模式编译我的 JS 文件时。 Otherwise, this works fine if the files are not compiled.否则,如果未编译文件,这将正常工作。

Not sure how to resolve this.不知道如何解决这个问题。

Here is the full order the code appears as in my script:这是代码在我的脚本中出现的完整顺序:

function Pagination() {
    let listArray         = new Array(); //store the collection of data to be sorted.
    let pageList          = new Array();  //keep track of the items to display on the current page only
    const numberPerPage   = 3;
    let currentPage       = 1;  //keep track of where we are in the pagination
    let numberOfPages     = 1;   // calculates the total number of pages
    const list            = document.querySelector('.url-list');
    let nextButton        = document.getElementById("next");
    const previousButton  = document.getElementById("previous");

    let bookMarksArray = window.localStorage.getItem('bookMarksArray') ? JSON.parse(window.localStorage.getItem('bookMarksArray')) : [];

    // CREATE BUTTONS FOR EACH PAGE THAT EXISTS
    function displayNumberedButtons(bookMarksArray) {
        for (let x = 0; x < bookMarksArray.length; x++)
        listArray.push(x);

        numberOfPages = Math.ceil(listArray.length / numberPerPage);

        let individualPagesArray = [];
        for (var i = 1; i <= numberOfPages; i++) {
            individualPagesArray.push(i);
        }

        for (var i = 0; i < individualPagesArray.length; i++) {
            document.getElementById("numbers").innerHTML += `<button id="${i+1}" onclick=showCurrentPage(${i+1})>` + individualPagesArray[i] + `</button>`;
        }
    }

    // CALCULATE WHEN PAGINATION SHOULD BEGIN AND STOP
    function paginationCountLogic(bookMarksArray) {
        let begin = ((currentPage - 1) * numberPerPage);
        let end = begin + numberPerPage;
        pageList = bookMarksArray.slice(begin, end);

        nextButton.disabled = currentPage === numberOfPages ? true : false;
        previousButton.disabled = currentPage === 1 ? true : false;
        displayBookmarks(pageList);
    }

    // DISPLAY BOOKMARKS
    function displayBookmarks(pageList) {
        list.innerHTML = "";
        for (let r = 0; r < pageList.length; r++) {
            list.innerHTML +=
            `<div>
                <form class="text animated slideInDown bookmarksForm" id=${pageList[r].name}>
                    <input class="nameItem" type="text" name="name" value=${pageList[r].name} id="name" placeholder="Name">
                    <input class="urlItem" type="url" name="url" value=${pageList[r].url} id="url" placeholder="https://example.com">
                    <button type="button" class="js-edit-url" id="edit">edit</button>
                    <button type="button" class="js-delete-url" id="delete">delete</button>
                </form>
            </div>`;
        }
    }

    // PAGINGATION CTAS
    window.showCurrentPage = (i) => {
        currentPage = i;
        paginationCountLogic(bookMarksArray);
    }

    window.nextPage = () => {
        currentPage += 1;
        paginationCountLogic(bookMarksArray);
    }

    window.previousPage = () => {
        currentPage -= 1;
        paginationCountLogic(bookMarksArray);
    }

    return {
      displayNumberedButtons,
      displayBookmarks,
      paginationCountLogic
    };
}

The cause is probably that the compiler doesn't see the function being called.原因可能是编译器没有看到被调用的函数。 Part of the advanced compilation is removing unused code and renaming the methods/variables.高级编译的一部分是删除未使用的代码并重命名方法/变量。

Within your js or html it is never called because the function call is only defined in a string value here :在您的 js 或 html 中,它永远不会被调用,因为函数调用仅在此处的字符串值中定义:

for (var i = 0; i < individualPagesArray.length; i++) {
   document.getElementById("numbers").innerHTML += `<button onclick=showCurrentPage(${i+1})>` + individualPagesArray[i] + `</button>`;
}

you can solve this pretty simply by rewriting this:你可以通过重写这个来解决这个问题:

window.showCurrentPage = (i) => {

to this:对此:

window['showCurrentPage'] = (i) => {

See : https://developers.google.com/closure/compiler/docs/api-tutorial3#removal请参阅: https : //developers.google.com/closure/compiler/docs/api-tutorial3#removal

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的ReferenceError :(函数)未在HTMLButtonElement.onclick中定义 - Uncaught ReferenceError: (function) not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick中定义查找 - Uncaught ReferenceError: lookup is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:在 HTMLButtonElement.onclick 中未定义 postAcmgForm - Uncaught ReferenceError: postAcmgForm is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:HTMLButtonElement.onclick中未定义submitToAPI - Uncaught ReferenceError: submitToAPI is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:id 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: id is not defined at HTMLButtonElement.onclick Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick - Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义函数 - Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick上定义toggleFunction - Uncaught ReferenceError: toggleFunction is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick上定义测试 - Uncaught ReferenceError: testing is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:异步未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: async is not defined at HTMLButtonElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM