2013-07-12 236 views
-3

我是JQuery的新手,儘管多次閱讀指南,但我找不到問題。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函數在單擊禁用按鈕時運行。警報在那裏測試它是否正常工作。

+6

什麼是不工作?你想做什麼? – putvande

+0

@ user2465313,明確你的問題... –

+0

我們不能確切地告訴你,不知道什麼不工作,但應該把查詢源放在你的腳本之前。這可能是錯誤的來源 – romainberger

回答

2

加載jQuery的第一個文件,更改文件的順序:

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

我已經嘗試過了。它仍然沒有工作 – user2465313

+0

奇怪。我再次嘗試這一點,它的工作。我會盡快接受答案。 – user2465313

1

也許是這樣的:

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

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

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

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

正如別人所說,扭轉腳本includement:

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

您的代碼看起來不錯,只需更改#enable click中的代碼即可運行該功能:

Demo

$("#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。

0

它改成這樣:

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

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

$(document).ready(function() { 

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

    $("#disable").click(function(){ 
     disable(); 
    }); 
}); 
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>