javascript onclick上的javascript ReferenceError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13264294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:15:33  来源:igfitidea点击:

javascript ReferenceError on onclick

javascriptreferenceerror

提问by Manoj Kumar

<html>
    <head>
       <script>
              function view_more(pg_no){
               alert(pg_no);
              }
       </script>
   </head>
  <body>
    <span onClick="view_more('1')"></span>
  </body>
</html>

Here, I can't understand why it say always [ReferenceError: view_more is not defined]

在这里,我不明白为什么总是说 [ReferenceError: view_more is not defined]

回答by Tapas Mahata

Try this first

先试试这个

<body>
<span onClick="alert('1');"></span>

if the above code works then there must exists others javascript which are actually got error prior to execute your code.

如果上述代码有效,则必须存在其他 javascript,它们在执行您的代码之前实际上已出错。

回答by kiero

I got the same problem today. My situation was like this:

我今天遇到了同样的问题。我的情况是这样的:

.html file:

.html 文件:

<head>
    <script type="text/javascript" src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="js/functions.js"></script>
</head>
<body>
    <div class="lc-form-holder">
        <form action="#" method="post">
        [...]
            <span onclick="myFunction();">NEXT</span>
        </form>
    </div>
</body>

and .js file:

和 .js 文件:

$(document).ready(function() {
    function myFunction() {
        console.log('click click');
    }
});

When I clicked on <span>I got an error Uncaught ReferenceError: myFuncion is not defined.

当我点击时<span>出现错误Uncaught ReferenceError: myFuncion is not defined

But when I do this in .js file (no document.ready):

但是当我在 .js 文件中执行此操作时(否document.ready):

    function myFunction() {
        console.log('click click');
    }

Everything work just fine.

一切正常。

I don't know is this helpful but I wanna share it if someone check this question in future.

我不知道这是否有帮助,但如果将来有人检查这个问题,我想分享它。

回答by swapnesh

<span onClick="view_more('1')">Test</span>

Add text and then click on text

添加文本,然后单击文本

Working at my end

在我的尽头工作

This is my code --

这是我的代码——

<html>
    <head>
       <script>
              function view_more(pg_no){
               alert(pg_no);
              }
       </script>
   </head>
  <body>
    <span onClick="view_more('1')">gfgf</span>
  </body>
</html>

EDIT

编辑

try using this code --

尝试使用此代码-

<html>
<head>
<script type="text/javascript">
function view_more(pg_no)
{
alert(pg_no);
}
</script>
</head>
<body>
<span onClick="javascript:view_more('1');">gfgf</span>
</body>
</html>