[英]jQuery .click() doesn't work offline?
我已將.click事件綁定到某些節標題上,以便用戶單擊這些標題之一時,該節就會出現/消失。
到目前為止還算不錯,但是由於某種原因,當我離線時,該操作不再有效。
我不了解這種行為,有人可以啟發我嗎?
這是代碼:
<script src="jquery-1.4.2.min.js" type="text/javascript" charset="utf-8"></script>
<script>
$(function(){
$("#contactInfoHeader").click(function(){
if($("#contactInformation").is(":visible")){
$("#contactInformation").fadeOut();
}else{
$("#contactInformation").fadeIn();
}
resizeWidget();
});
});
</script>
<h3 id="contactInfoHeader">Contact Information</h3>
<div id="contactInformation">Telephone:XXXXXX</div>
親愛的,您使用的是簡單的jQuery,它使用其在線文件。 因此,當您處於聯機狀態時,該功能正常工作,但在離線狀態下,該按鈕將停止工作。 而已。 很簡單
在jQuery 1.4.x中,應使用此格式將處理程序附加到ready事件。
$(document).ready(function(){
//add your stuff here
});
或者您可以保留$(handler); 語法, 但您需要使用結尾paren 。
<script>
$(function(){
$("#contactInfoHeader").click(function(){
if($("#contactInformation").is(":visible")){
$("#contactInformation").fadeOut();
}else{
$("#contactInformation").fadeIn();
}
resizeWidget();
});
});//ADDED ")" here!
</script>
您有一些代碼不一致,或者您誤認為它可以工作。
那也應該在document.ready中:
$(function() {
/* your code */
});
否則,它不會將click事件附加到任何東西,因為元素在運行時不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.