如果点击其他按钮不起作用

0 jquery

我点击按钮时尝试做某事,下面的示例代码.

小提琴

$(":button").click(function() {
   if ($(this).prop("id") == "update") {
        alert("Update clicked");
    }
    else {
        alert("Add clicked");   
    }
}); 
Run Code Online (Sandbox Code Playgroud)

这适用于小提琴,但奇怪的是在我的localhost上不起作用.

Uncaught SyntaxError: UnExpected token Illegal在这一行得到一个错误():

if ($(this).prop("id") == "update") {
Run Code Online (Sandbox Code Playgroud)

我正在使用jQuery 1.10.0.

知道为什么吗?

小智 5

这对我有用.请在浏览器中查看此内容.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-1.10.0.min.js"></script>
    <script>
    $(document).ready(function(){
        $(":button").click(function(){
           if($(this).prop("id") == "update"){
                alert("Update clicked");
            }
            else {
                alert("Add clicked");   
            }
        });
    });
    </script>
</head>
<body>
<button id="update" type="button">UPDATE</button>
<button id="add" type="button">ADD</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)