简体   繁体   English

HTML无法运行JavaScript函数

[英]HTML won't run JavaScript function

The issue it that I can't get a simple JavaScript function to run with HTML. 我无法使用HTML运行简单的JavaScript函数。 I get an error that says "Uncaught ReferenceError: getYear is not defined".Thanks! 我收到一条错误消息:“未捕获的ReferenceError:未定义getYear”。谢谢!

 $(document).ready(function () { function getYear(){ document.write(new Date().getFullYear()); } }); 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="js/script.js"></script> </head> <body> <footer> <p>&copy; <script> getYear()</script> | All rights reserved</p> </footer> </body> </html> 

I believe that's because you wrapped the function in your js file in a document.ready state so you're unable to access the function inside your HTML footer tag because of that. 我相信这是因为您将函数包装在js文件中的document.ready状态下,因此无法访问HTML页脚标记内的函数。

removing the document.ready will solve the issue. 删除document.ready将解决此问题。

your new code should be 您的新代码应为

  function getYear(){
    document.write(new Date().getFullYear());
  }

You have to remove ready function, because this function waits that all page onload, only use: 您必须删除ready函数,因为此函数等待所有页面加载,仅使用:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> function getYear(){ document.write(new Date().getFullYear()); } </script> </head> <body> <footer> <p>&copy; <script> getYear()</script> | All rights reserved</p> </footer> </body> </html> 

Try the following code; 试试下面的代码;

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="js/script.js"></script> </head> <body> <footer> <p> &copy; <script> document.write(new Date().getFullYear()); </script>| All rights reserved </p> </footer> </body> </html> 

You would not need the 您不需要

 $(document).ready(function () {...

Your new code should be like: 您的新代码应类似于:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>


<script>

function getYear(){ return  new Date().getFullYear();  }

</script>

</head>

<body>
    <footer>
        <p>&copy; <script> getYear()</script> | All rights reserved</p>
    </footer>
</body>
</html>

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

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