【发布时间】:2014-05-12 02:51:46
【问题描述】:
我正在慢慢熟悉 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>
【问题讨论】:
标签: javascript jquery jquery-selectors