[英]Abstracting the adding of click events to elements selected by class using jQuery
我正在慢慢了解jQuery,並開始想抽象我的代碼。 我在嘗試定義頁面加載時的點擊事件時遇到了問題。
在下面的代碼中,我試圖通過“ block”類遍歷每個div,並通過按類選擇事件,將事件添加到其某些子元素中:
<script language="javascript" type="text/javascript">
$(document).ready(function (){
$('HTML').addClass('JS'); // if JS enabled, hide answers
$(".block").each(function() {
problem = $(this).children('.problem');
button = $(this).children('.showButton');
problem.data('currentState', 'off');
button.click(function() {
if ((problem.data('currentState')) == 'off'){
button.children('.btn').html('Hide');
problem.data('currentState', 'on');
problem.fadeIn('slow');
} else if ((problem.data('currentState')) == 'on'){
button.children('.btn').html('Solve');
problem.data('currentState', 'off');
problem.fadeOut('fast');
}
return false;
});
});
});
</script>
<style media="all" type="text/css">
.JS div.problem{display:none;}
</style>
<div class="block">
<div class="showButton">
<a href="#" title="Show solution" class="btn">Solve</a>
</div>
<div class="problem">
<p>Answer 1</p>
</div>
</div>
<div class="block">
<div class="showButton">
<a href="#" title="Show solution" class="btn">Solve</a>
</div>
<div class="problem">
<p>Answer 2</p>
</div>
</div>
不幸的是,使用此功能時,只有div的最后一個按鈕才有效。 該事件不是“本地化的”(如果它是正確的詞?),即該事件僅應用於每種方法中的最后一個$(“。block”)。
因此,我必須努力為每個元素添加ID,然后逐個定義我的點擊事件。 當然,有更好的方法! 誰能告訴我我在做什么錯? 以及如何擺脫對這些ID的需求(我希望它可以在動態生成的頁面上工作,在這些頁面上我可能不知道有多少個“塊” ...)
<script language="javascript" type="text/javascript">
$(document).ready(function (){
$('HTML').addClass('JS'); // if JS enabled, hide answers
// Preferred version DOESN'T' WORK
// So have to add ids to each element and laboriously set-up each one in turn...
$('#problem1').data('currentState', 'off');
$('#showButton1').click(function() {
if (($('#problem1').data('currentState')) == 'off'){
$('#showButton1 > a').html('Hide');
$('#problem1').data('currentState', 'on');
$('#problem1').fadeIn('slow');
} else if (($('#problem1').data('currentState')) == 'on'){
$('#showButton1 > a').html('Solve');
$('#problem1').data('currentState', 'off');
$('#problem1').fadeOut('fast');
}
return false;
});
$('#problem2').data('currentState', 'off');
$('#showButton2').click(function() {
if (($('#problem2').data('currentState')) == 'off'){
$('#showButton2 > a').html('Hide');
$('#problem2').data('currentState', 'on');
$('#problem2').fadeIn('slow');
} else if (($('#problem2').data('currentState')) == 'on'){
$('#showButton2 > a').html('Solve');
$('#problem2').data('currentState', 'off');
$('#problem2').fadeOut('fast');
}
return false;
});
});
</script>
<style media="all" type="text/css">
.JS div.problem{display:none;}
</style>
<div class="block">
<div class="showButton" id="showButton1">
<a href="#" title="Show solution" class="btn">Solve</a>
</div>
<div class="problem" id="problem1">
<p>Answer 1</p>
</div>
</div>
<div class="block">
<div class="showButton" id="showButton2">
<a href="#" title="Show solution" class="btn">Solve</a>
</div>
<div class="problem" id="problem2">
<p>Answer 2</p>
</div>
</div>
編輯,因為我發現了HTML :)
首先:您需要使用var
關鍵字聲明“ button”和“ problem”。
接下來,將在目標元素的上下文中調用單擊處理程序,因此您不應在其內部引用“按鈕”,而應引用“ $(this)”。
接下來,您將需要提出一種將每個按鈕與其對應的“問題”元素相關聯的方法。 處理程序可以使用jQuery DOM遍歷例程找到伴隨的“問題”:
button.click(function() {
var $button = $(this), $problem = $button.closest('div.block').find('.problem');
if (($problem.data('currentState')) == 'off'){
$button.children('.btn').html('Hide');
$problem.data('currentState', 'on');
$problem.fadeIn('slow');
} else if (($problem.data('currentState')) == 'on'){
$button.children('.btn').html('Solve');
$problem.data('currentState', 'off');
$problem.fadeOut('fast');
}
return false;
});
現在,另一種替代方法是使用live
機制,該機制僅允許您創建一個處理函數。 這將非常相似,但是您無需將處理程序綁定到每個“按鈕” <div>
,而是對其進行設置並引用選擇器:
$('.button').live('click', function() {
// as above
});
抱歉,沒有足夠的聲譽來投票贊成以上任何一項,而它們絕對是對的!
答案實際上很簡單-正如@patrick和@Pointy正確指出的,我需要做的就是添加var關鍵字來聲明我的兩個變量。 h!
我實際上無法獲得代碼建議,但是由於您的幫助已將其修復,因此我的工作正常。 謝謝!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function (){
$('HTML').addClass('JS'); // if JS enabled, hide answers
$(".block").each(function() {
var problem = $(this).children('.problem');
var button = $(this).children('.showButton');
problem.data('currentState', 'off');
button.click(function() {
if ((problem.data('currentState')) == 'off'){
button.children('.btn').html('Hide');
problem.data('currentState', 'on');
problem.fadeIn('slow');
} else if ((problem.data('currentState')) == 'on'){
button.children('.btn').html('Solve');
problem.data('currentState', 'off');
problem.fadeOut('fast');
}
return false;
});
});
});
</script>
<style media="all" type="text/css">
.JS div.problem{display:none;}
</style>
</head>
<body>
<div class="block">
<div class="showButton">
<a href="#" title="Show solution" class="btn">Solve</a>
</div>
<div class="problem">
<p>Answer 1</p>
</div>
</div>
<div class="block">
<div class="showButton">
<a href="#" title="Show solution" class="btn">Solve</a>
</div>
<div class="problem">
<p>Answer 2</p>
</div>
</div>
<div class="block">
<div class="showButton">
<a href="#" title="Show solution" class="btn">Solve</a>
</div>
<div class="problem">
<p>Answer 3</p>
</div>
</div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.