Uncaught ReferenceError: html is not defined 未捕获的引用错误:未定义html

huangapple go评论63阅读模式
英文:

Uncaught ReferenceError: html is not defined

问题

以下是您要翻译的代码部分:

(function () {
    const fonts = ['cursive', 'sans-serif', 'serif', 'monospace'];
    let captchaValue = "";
    function generateCaptcha(){
        let value = btoa(Math.random()*100000000);
        value = value.substr(0,5+Math.random()*5);
        captchaValue = value;
    }

    function setCaptcha() {
        captchaValue.split("").map((char) => {
            const rotate = -20 + Math.trunc(Math.random()*30);
            const font = Math.trunc(Math.random()*fonts.length);
            return `<span
                style="
                transform:rotate(${rotate}deg);
                font-family: ${fonts[font]};
            "                
            >
                ${char}
            </span>`;
        }).join("");
        document.querySelector(".login-form .captcha .preview").innerHTML = html;
    }
    function initCaptcha(){
        document.querySelector(".login-form .captcha .captcha-refresh").addEventListener('click', function(){
            generateCaptcha();
            setCaptcha();
        });
        generateCaptcha();
        setCaptcha();
    }
    initCaptcha();
})();
英文:

I am just trying to make a login page that using captcha using javascript language here. But it throw an error when I tried to produce it.

It was saying that html is not defined, I already tried to check the code, but still could not figure it out what went wrong here.

here is the Javascript code that I think the problem spot is:

(function () {
    const fonts =[&#39;cursive&#39;, &#39;sans-serif&#39;, &#39;serif&#39;, &#39;monospace&#39;];
    let captchaValue    = &quot;&quot;;
    function generateCaptcha(){
        let value = btoa(Math.random()*100000000);
        value = value.substr(0,5+Math.random()*5);
        captchaValue= value;
    }

    function setCaptcha() {
        captchaValue.split(&quot;&quot;).map((char)=&gt; {
            const rotate = -20 + Math.trunc(Math.random()*30);
            const font = Math.trunc(Math.random()*fonts.length);
            return `&lt;span
                style=&quot;
                transform:rotate(${rotate}deg);
                font-family: ${fonts[font]};
            &quot;                
            &gt;
                ${char}
            &lt;/span&gt;`;
        }).join(&quot;&quot;);
        document.querySelector(&quot;.login-form .captcha .preview&quot;).innerHTML = html;
    }
    function initCaptcha(){
        document.querySelector(&quot;.login-form .captcha .captcha-refresh&quot;).addEventListener(&#39;click&#39;, function(){
            generateCaptcha();
            setCaptcha();
        });
        generateCaptcha();
        setCaptcha();
    }
    initCaptcha();
})();

答案1

得分: -2

你的问题似乎出在这行代码:

document.querySelector(".login-form .captcha .preview").innerHTML = html;

你将.preview元素的innerHTML设置为了一个名为html的变量,但在你的代码中并没有定义这个变量。

我不确定你想要将它设置为什么,但这只是一个猜测,你可以将setCaptcha 更改为以下内容:

function setCaptcha() {
    let html = captchaValue.split("").map((char)=> {
        const rotate = -20 + Math.trunc(Math.random()*30);
        const font = Math.trunc(Math.random()*fonts.length);
        return `<span
            style="
            transform:rotate(${rotate}deg);
            font-family: ${fonts[font]};
        "                
        >
            ${char}
        </span>`;
    }).join("");
    document.querySelector(".login-form .captcha .preview").innerHTML = html;
}
英文:

Your problem seems to be in this line of code:

document.querySelector(&quot;.login-form .captcha .preview&quot;).innerHTML = html;

you are setting the innerHTML of the ".preview" element to html which you didn't define anywhere in your code.

I'm not sure of what you want to set it to, but this is merely a guess, you can change the setCaptcha to the following:

function setCaptcha() {
    let html = captchaValue.split(&quot;&quot;).map((char)=&gt; {
        const rotate = -20 + Math.trunc(Math.random()*30);
        const font = Math.trunc(Math.random()*fonts.length);
        return `&lt;span
            style=&quot;
            transform:rotate(${rotate}deg);
            font-family: ${fonts[font]};
        &quot;                
        &gt;
            ${char}
        &lt;/span&gt;`;
    }).join(&quot;&quot;);
    document.querySelector(&quot;.login-form .captcha .preview&quot;).innerHTML = html;
}

huangapple
  • 本文由 发表于 2023年2月18日 17:46:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定