函数未定义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>
更多推荐
发布评论