这是一个简单的问题,我敢肯定,但我想不出如何让它工作。我已经尝试了.next()和.nextAll(),但都没有用。我能想到的唯一一件事是,由于代码的流动,我正在尝试的函数不能工作。不管怎样,下面是jquery和html。我只是想让表单框在点击下载时淡入。
HTML:
<div class="pdfs">
<div class="pdf-left">
<div class="image">
<img src="images/pdfs/macrolux/1.jpg" width="100" height="79">
</div>
<div class="text">
<div class="view">View</div>
<div class="download">Download</div>
</div>
</div>
<div class="pdf-right">
<div class="image">
<img src="images/pdfs/macrolux/2.jpg" width="100" height="79">
</div>
<div class="text">
<div class="view">View</div>
<div class="download">Download</div>
</div>
</div>
</div>
<div class="form-box">
Form
</div>jQuery:
$(document).ready(function(){
$('div.download').on('click', function(){
$(this).next('div.form-box').fadeIn('slow');
});
});发布于 2013-01-20 20:38:35
公认的答案是脆弱的,如果您的文档结构发生变化,您需要重写jquery。
更通用的解决方案是提供表单框div和您的下载div相关的ids,例如
<div id="download-1" class="download">
Download<
</div> 和
<div id="download-1_formbox" class="form-box">
Form
</div>`然后在您的jquery中
$(document).ready(function(){
$('div.download').on('click', function(){
var divId=$(this).attr("id");
$("#" + divId + "_formbox").fadeIn('slow');
});
});更新:根据请求,我已经添加了一些代码来切换,工作小提琴是here /(只有第一个下载链接有效,没有给第二个链接一个id)
发布于 2013-01-20 20:24:51
你没有$(this).next(),你可以在chrome/firefox中测试它:
console.log($('div.download').next());要获得正确的div,请尝试:
$(document).ready(function(){
$('div.download').on('click', function(){
$(this).parent().parent().parent().next().fadeIn('slow');
});
});或者,如果您只想隐藏一个form-box:
$(document).ready(function(){
$('div.download').on('click', function(){
$('div.form-box').fadeIn('slow');
});
});发布于 2013-01-20 20:25:10
如果您没有多个div.form-box,则This就足够了
$(document).ready(function(){
$('div.download').on('click', function(){
$('div.form-box').fadeIn('slow');
});
});如果你在每个pdfs div之后都有div.form-box:
$(document).ready(function(){
$('div.download').on('click', function(){
$(this).parents('.pdfs').next('div.form-box').fadeIn('slow');
});
});https://stackoverflow.com/questions/14424459
复制相似问题