[英]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.