简体   繁体   English

使用jquery的javascript函数没有被调用

[英]javascript function that uses jquery isn't being called

I'm woking on a ASP.NET project and im trying to use the attr function, when im putting in the script tag the src for the jquery file the function isnt being called when im calling it via c# code. 我在一个ASP.NET项目中工作,我试图使用attr函数,当我在script标签中将jquery文件的src放入src时,我通过c#代码调用该函数时,该函数没有被调用。

 <script type ="text/javascript" src="jquery-3.1.1.min.js">
    $(document).ready(function () {
        function startGame(path) {
            alert(path);
            $("#test").attr("style", "color:Blue");
            $("#game_param").attr("value", path);
            $("#game_embed").attr("src", path);
        }
    });   
</script>

I tried to write a simple javascript fucntion 我试图写一个简单的javascript功能

<script type ="text/javascript" src="jquery-3.1.1.min.js">
    function startGame(a) {
        alert(a);
    }
</script>

and it wasn't called either 也没有被称为

im getting en error that startGame isn't defined, in both cases. 在这两种情况下,我都会收到未定义startGame的错误消息。 can someone expain to me what im doing wrong? 有人可以向我解释我做错了什么吗?

You need to include the jQuery source separately from your custom code. 您需要将jQuery源代码与自定义代码分开添加。 Otherwise I believe what is inside of the script tag is ignored. 否则,我相信脚本标记内的内容将被忽略。

 <script type ="text/javascript" src="jquery-3.1.1.min.js"></script>
 <script type="text/javascript">
  $(document).ready(function () {
    var p = 'here/is/my/path';
    function startGame(path) {
        alert(path);
        $("#test").attr("style", "color:Blue");
        $("#game_param").attr("value", path);
        $("#game_embed").attr("src", path);
    }
    startGame(p);
 });
 </script>

Your Javascript isn't correct. 您的Javascript不正确。

When you do: 当您这样做时:

<script type ="text/javascript" src="jquery-3.1.1.min.js">
  function startGame(a) {
    alert(a);
  }
</script>

You're setting the source of the JavaScript file to be the jQuery JS file. 您正在将JavaScript文件的源设置为jQuery JS文件。 If you want to define your own functions, you have to put it in its own script tag, like so: 如果要定义自己的函数,则必须将其放在自己的script标签中,如下所示:

<script type ="text/javascript" src="jquery-3.1.1.min.js"></script>
<script type="text/javascript">
    function startGame(a) {
        alert(a);
    }
</script>

You actually have multiple errors. 您实际上有多个错误。

  1. Browsers will ignore any javascript written inside a script tag that has a src attribute. 浏览器将忽略在具有src属性的script标签内编写的任何javascript。 So you need to include jQuery separately, like so: 因此,您需要单独包含jQuery,如下所示:

     <script type ="text/javascript" src="jquery-3.1.1.min.js"></script> <script> // Your code here. </script> 
  2. You're never actually calling the function, only declaring them. 您永远不会真正调用该函数,而只是声明它们。 If you're only going to use this once, you don't even need a function: 如果只使用一次,则甚至不需要一个函数:

     <script type ="text/javascript" src="jquery-3.1.1.min.js"></script> <script> $(document).ready(function () { alert(path); $("#test").attr("style", "color:Blue"); $("#game_param").attr("value", path); $("#game_embed").attr("src", path); }); </script> 

    Otherwise, you need to call the function after declaring it like so: 否则,您需要像这样声明后调用该函数:

     $(document).ready(function () { function startGame(path) { // Your code } startGame(); }); 

Function Declarations vs Expressions 函数声明与表达式

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

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