【问题标题】:Javascript in Visual studio Web ApiVisual Studio Web API 中的 Javascript
【发布时间】:2013-12-16 17:33:59
【问题描述】:

我的 html 代码中嵌入了一个 javascript 函数,但是当我运行该页面时出现此错误:Uncaught ReferenceError: myFunction is not defined。

我也在使用 angularjs,所以我有我的索引页面,正如他们解释的那样,我的部分页面,并且该功能在我的部分页面中。

当我从文件夹中双击网页时,我可以运行脚本,但是当我运行 web api 时,它会出现该错误。

我的javascript函数:

<script type="text/javascript">
      function myFunction() {
          var x;
          var r = confirm("Press a button!");
          if (r == true) {
              x = "You pressed OK!";
          }
          else {
              x = "You pressed Cancel!";
          }
          document.getElementById("demo").innerHTML = x;
      }

我怎么称呼它:

<button onclick="myFunction()">Try it</button>

谁能帮助我? 想

【问题讨论】:

标签: c# javascript angularjs


【解决方案1】:

它似乎在加载之前就尝试使用该功能。你可以试试下面。

<script type="text/javascript">
      function myLoad() {
          var myButton = document.getElementById('myButton');

          myButton.addEventListener('click', function() {
             var x;
             var r = confirm("Press a button!");
             if (r == true) {
                x = "You pressed OK!";
             }
             else {
                x = "You pressed Cancel!";
             }

             document.getElementById("demo").innerHTML = x;
          }, false);
       }

       window.load = myLoad;
</script>

最后给按钮添加一个Id。

<button id="myButton">Try it</button>

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2022-11-17
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多