jQuery按钮单击不会运行

时间:2013-04-17 18:55:59

标签: jquery onclick

我无法让这个简单的脚本运行并且暂时盯着它看一段时间并且没有看到任何语法错误。只需要点击即可显示警报。

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type='text/javascript'>

$(document).ready(function() {
    $("#authenticate_button").click(function() {
        alert("click");
    });
});

</script>

</head>
<body>
    <input id="code" type="text"> 
    <button id='authenticate_button'>Authenticate</button>
</body>
</html>

2 个答案:

答案 0 :(得分:6)

您正在使用网络浏览器直接打开文件。 //是您当前协议(file://)的简写,因此jQuery不会从Google的CDN加载。

您需要在此行http:之前添加//来明确指定协议:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

我还要添加一个doctype:

<!DOCTYPE html>

答案 1 :(得分:0)

你也可以试试这个:

使用MediaTemple提供的jQuery CDN

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

使用Microsoft的CDN

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>

作为jQuery js文件的替代。

我建议你这样做:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>  
<script>  
     // Fallback to loading jQuery from a local path if the CDN is unavailable  
     (window.jQuery || document.write('<script src="/scripts/jquery-1.9.0.min.js"><\/script>'));  
</script>