HTML按钮不调用函数

时间:2016-03-29 23:43:24

标签: javascript html html5

这是我的第一次javascript尝试,我在Chrome控制台中获得以下内容:

"未捕获的ReferenceError:未定义test1。"

我尝试通过onclick按钮事件发送Ajax请求并在文档中显示数据。但是,由于这个错误,我只是为了调试尝试而回到了hello-world。请原谅这类问题,因为我知道它非常基本。

感谢任何帮助。谢谢你的时间!

<!DOCTYPE html>
<html>
<head>
</head> 
<body>

<button onclick="test1()">Click Here</button>
<p id="Data"></p>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">

function test1(){
alert("Hello, World");
/* $.ajax({
    url: Target_URL,
    headers: {
        'Accept':'application/json',
        'authorization', 'Bearer ' + vToken
    },
    method: 'GET'
   // dataType: 'json',
   // data: YourData,
    success: ajaxCall = data
  }); */
}
</script>

</body>

</html>

5 个答案:

答案 0 :(得分:7)

您只使用了一个script标记来加载jquery和您的方法 - 这不起作用...... "If a script element has a src attribute specified, it should not have a script embedded inside its tags."这里有一个可行的解决方案:

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
</head> 
<body>

<button onclick="test1()">Click Here</button>
<p id="Data"></p>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
function test1(){
alert("Hello, World");
/* $.ajax({
    url: Target_URL,
    headers: {
        'Accept':'application/json',
        'authorization', 'Bearer ' + vToken
    },
    method: 'GET'
   // dataType: 'json',
   // data: YourData,
    success: ajaxCall = data
  }); */
}
</script>

</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您无法将javascript代码放在链接外部文件的<script>标记中(它不应包含在<script src="...">函数</script>中)。您的test1函数应该与scr javascript标记分开。试试这个:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">/<script>
<script> your javascript function should be here </script>

希望这能解决您的问题。

答案 2 :(得分:-1)

将JavaScript放在文档的head中,并分离出包含jquery和实际代码的脚本。

<!DOCTYPE html>
<html>
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 

<script>
function test1(){
    alert("Hello, World");
}
</script>
</head> 
<body>
    <button onclick="test1()">Click Here</button>
    <p id="Data"></p>
</body>
</html>

答案 3 :(得分:-1)

您还没有将javascript方法放在正确的javascript标记中,单独放置javascript方法和javascript src,请在下面查看以获得更多说明:

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<script type="text/javascript">
  function test1() {
    alert("Hello, World");
  }
/* $.ajax({
    url: Target_URL,
    headers: {
        'Accept':'application/json',
        'authorization', 'Bearer ' + vToken
    },
    method: 'GET'
   // dataType: 'json',
   // data: YourData,
    success: ajaxCall = data
  }); */
</script>

</head> 
<body>

<button onclick="test1()">Click Here</button>
<p id="Data"></p>

</body>

</html>

答案 4 :(得分:-3)

首先,您需要在调用之前声明JavaScript函数。

更改为:

<!DOCTYPE html>
<html>
<head>
</head> 
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js" />
<script>
    function test1(){
        alert("Hello, World");
    }
</script>

<button onClick="test1()">Click Here</button>
<p id="Data"></p>

</body>

</html>
相关问题