函数未定义onclick javascript [关闭](function not defined onclick javascript [closed])

编程入门 行业动态 更新时间:2024-10-12 01:24:36
函数未定义onclick javascript [关闭](function not defined onclick javascript [closed])

我有这样的代码:

<script> function logout() { var logoutbtn = document.getElementById('logouttext'); logoutbtn.innerText = "Byebye!"; setTimeout(function () { window.location.href = "/"; } 1000); } </script> <div id="Logout" class="waves-effect logout" onclick="logout()"> <p id="logouttext">LOGOUT</p> </div>

现在,当我单击logout div时,它会在控制台中输出函数logout未定义,但是,当我将函数更改为仅包含window.location.href = "/"; 它确实有效。 我在这里做错了什么?

I have this code:

<script> function logout() { var logoutbtn = document.getElementById('logouttext'); logoutbtn.innerText = "Byebye!"; setTimeout(function () { window.location.href = "/"; } 1000); } </script> <div id="Logout" class="waves-effect logout" onclick="logout()"> <p id="logouttext">LOGOUT</p> </div>

Now when I click on the logout div, it prints in the console that the function logout is not defined, however, when I change the function to only contain window.location.href = "/"; it does work. What am I doing wrong here?

最满意答案

你在1000毫秒之前错过了逗号。

在工作代码下方:

function logout() {
    var logoutbtn = document.getElementById('logouttext');
    logoutbtn.innerText = "Byebye!";
    setTimeout(function () {
        window.location.href = "/";
    },1000);
}

             
  
<div id="Logout" class="waves-effect logout" onclick="logout()"><p id="logouttext">LOGOUT</p></div> 
  
 

You missed to put comma , before the 1000 milliseconds .

Below a working code :

function logout() {
    var logoutbtn = document.getElementById('logouttext');
    logoutbtn.innerText = "Byebye!";
    setTimeout(function () {
        window.location.href = "/";
    },1000);
}

             
  
<div id="Logout" class="waves-effect logout" onclick="logout()"><p id="logouttext">LOGOUT</p></div> 
  
 

更多推荐

本文发布于:2023-08-06 23:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   未定义   onclick   javascript   defined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!