-3

我是 JQuery 的新手,尽管多次阅读指南,但我找不到问题所在。

当我单击按钮时,以下功能不会被触发:

function enable(){
    alert("Enable Working!");
}

function disable(){
    alert("Disable Working!");
}

$("#enable").click(function(){
    $("#enable").hide();
});
$("#disable").click(function(){
    disable();
});

和html页面。我在这里也没有看到任何错误

<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>popup</title>
    <style>
        #enable{
            color:green;
            position:relative;
            top:20px;
            }
        #disable{
            color:red;
            position:relative;
            top:25px;
            }
        body{
            height: 100px;
        }
    </style>


</head>
<body id="popup">
    <input id="enable" type="button" value="Enable">
    <br />
    <input id="disable" type="button" value="Disable">

    <script src="popup.js"></script>
    <script src="jquery.js"></script>


</body>
<html>

我想要的是enable当我单击启用按钮时该功能运行。我希望disable在单击禁用按钮时运行该功能。警报用于测试它是否正常工作。

4

5 回答 5

2

首先加载 jQuery 文件,更改文件的顺序:

<script src="jquery.js"></script>
<script src="popup.js"></script>
于 2013-07-12T08:57:26.717 回答
1

也许是这样的:

function enable(){
    alert("Enable Working!");
}

function disable(){
    alert("Disable Working!");
}

$("#enable").click(enable);

$("#disable").click(disable);

正如其他人提到的,反转脚本包含:

<script src="jquery.js"></script>
<script src="popup.js"></script>
于 2013-07-12T08:59:03.567 回答
1

您的代码看起来不错,只需更改#enable click 内的代码即可运行该函数:

演示

$("#enable").click(function(){
    enable();
});
$("#disable").click(function(){
    disable();
});

如果要隐藏按钮,请将其放入函数中:

function enable(){
    alert("Enable Working!");
    $("#enable").hide();
}

添加这个以在页面加载时加载您的代码:

$(document).ready(function(){
//your code
});

可以从在线资源加载 jQuery 脚本文件。如果你想要代码是<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>. 在其他 .js 文件之前加载它。如果需要 jQuery。

于 2013-07-12T08:59:08.943 回答
0

将其更改为:

function enable(){
    alert("Enable Working!");
}

function disable(){
    alert("Disable Working!");
}

$(document).ready(function() {

    $("#enable").click(function(){
        enable();
    });

    $("#disable").click(function(){
        disable();
    });
});
于 2013-07-12T09:06:32.740 回答
0
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
       <title>popup</title>
  <style>
      #enable{
         color:green;
            position:relative;
            top:20px;
            }
        #disable{
           color:red;
          position:relative;
          top:25px;
             }
     body{
             height: 100px;
      }
 </style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){

 });
function enable(){
   alert("Enable Working!");
}

function disable(){
  alert("Disable Working!");
}

$("#enable").click(function(){

 enable();
});
$("#disable").click(function(){
    disable();
});
});
</script>
</head>
<body>
<input id="enable" type="button" value="Enable">
    <br />
    <input id="disable" type="button" value="Disable">
</body>
</html>
于 2013-07-12T09:13:20.980 回答