简体   繁体   English

在 function 外部调用时出现未定义的变量,但在 function 内部调用时工作

[英]Variable coming up as undefined when called outside of a function, but working when called inside the function

I have a function that creates an html dom modal window with a textbox and a button that closes it.我有一个 function 创建一个 html dom modal window 带有一个文本框和一个关闭它的按钮。 When the close button is clicked, the value of the textbox is stored in a variable.单击关闭按钮时,文本框的值存储在一个变量中。 The variable (as far as I know) is global, and is not limited to inside the modal window function. I have it set up so that when the close button is clicked, 2 alerts are opened in succession, both with the variable as their message.该变量(据我所知)是全局的,并且不限于模态 window function 内部。我将其设置为当单击关闭按钮时,连续打开 2 个警报,均以变量作为它们的信息。 The first alert is called from within the modal window function, and its message reads as the value of the textbox.第一个警报从模态 window function 中调用,其消息读取为文本框的值。 The second alert is called from outside the function, and it just says "undefined".第二个警报是从 function 外部调用的,它只是说“未定义”。 How can I make it so that when called outside of the function, the variable doesn't come up as undefined?我怎样才能做到在 function 之外调用时,变量不会出现未定义的情况?

edit for clarification: this code will be run as a javascript bookmarklet that injects the modal window into a webpage, and that the code i showed is currently the full extent of the code that will be run.编辑澄清:此代码将作为 javascript 小书签运行,它将模式 window 注入网页,并且我显示的代码目前是将要运行的代码的完整范围。

 (async () => { let textValue; let myAlert = async(bodyText) => { return new Promise((resolve) => { let style = document.createElement('style'); const GUI = document.createElement('div'); GUI.appendChild(style); GUI.style.width = '400px'; //GUI.style.height = '500px'; GUI.style.background = 'hsl(0, 0%, 10%)'; GUI.style.position = 'absolute'; GUI.style.textAlign = 'center'; GUI.style.color = 'white'; GUI.style.top = '0px'; GUI.style.left = '0px'; let header = document.createElement('div'); GUI.appendChild(header); header.style.width = '100%'; header.style.height = '35px'; header.style.paddingTop = '2px'; header.style.fontSize = '1.5rem'; header.style.textAlign = 'center' header.innerText = 'Alert'; let loop; let close = document.createElement('button'); header.appendChild(close); close.style.height = '45px'; //close.style.paddingTop = '10px'; //close.style.paddingRight = '15px'; close.style.position = 'absolute'; close.style.top = '0px'; close.style.right = '0px'; close.innerText = 'OK'; close.onclick = () => { var textValue = document.getElementById('inputText').value; alert(textValue); resolve(); GUI.remove(); clearInterval(loop); }; let body = document.createElement('div'); GUI.appendChild(body); body.innerHTML = bodyText; body.style.minHeight = '70px'; body.id = 'body'; let lineBreak = document.createElement('br') body.appendChild(lineBreak); let inputText = document.createElement('input'); body.appendChild(inputText); inputText.type = 'text'; inputText.value = 'value'; inputText.id = 'inputText'; document.body.append(GUI); }); }; (async function() { await myAlert('Alert'); alert(textValue); })(); })();

textValue is outside of that function's scope, try this: textValue 在该函数的 scope 之外,试试这个:

let textValue;
    (async () => {
        let myAlert = async(bodyText) => {
            return new Promise((resolve) => {
                let style = document.createElement('style');
                const GUI = document.createElement('div');
                GUI.appendChild(style);
                GUI.style.width = '400px';
                //GUI.style.height = '500px';
                GUI.style.background = 'hsl(0, 0%, 10%)';
                GUI.style.position = 'absolute';
                GUI.style.textAlign = 'center';
                GUI.style.color = 'white';
                GUI.style.top = '0px';
                GUI.style.left = '0px';
                let header = document.createElement('div');
                GUI.appendChild(header);
                header.style.width = '100%';
                header.style.height = '35px';
                header.style.paddingTop = '2px';
                header.style.fontSize = '1.5rem';
                header.style.textAlign = 'center'
                header.innerText = 'Alert';
                let loop;
                let close = document.createElement('button');
                header.appendChild(close);
                close.style.height = '45px';
                //close.style.paddingTop = '10px';
                //close.style.paddingRight = '15px';
                close.style.position = 'absolute';
                close.style.top = '0px';
                close.style.right = '0px';
                close.innerText = 'OK';
                close.onclick = () => {
                    var textValue = document.getElementById('inputText').value;
                    alert(textValue);
                    resolve();
                    GUI.remove();
                    clearInterval(loop);
                };
                let body = document.createElement('div');
                GUI.appendChild(body);
                body.innerHTML = bodyText;
                body.style.minHeight = '70px';
                body.id = 'body';
                let lineBreak = document.createElement('br')
                body.appendChild(lineBreak);
                let inputText = document.createElement('input');
                body.appendChild(inputText);
                inputText.type = 'text';
                inputText.value = 'value';
                inputText.id = 'inputText';
                document.body.append(GUI);
            });
        };
        (async function() {
            await myAlert('Alert');
            alert(textValue);
        })();
    })();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM