我有四个 div,可以通过标题切换(正如你在下面看到的,我展示了一组)。每个标题链接类与文本字段类之前的类相匹配。我不想分别编写四次 javascipt,而是希望使用 $this 来编写一些更干燥的东西来定位当前类,然后将当前类作为类文本。
important_info_header 类将是每个 div 的动态类。
这种写作不太好,所以欢迎任何帮助
尝试失败
$(this).click(function() {
$(this+'.text').slideToggle('slow', function() {
//ani complete.
});
});
工作标记
HTML 开始
<h4><a href="javascript:void(0)" class="important_info_header"><%= t("catalog_items.show_lot.important_info_header")%></a></h4>
<div class="important_info_header text">
<p><%= t("catalog_items.show_lot.important_info") %></p>
</div><!-- /text -->
<h4><a href="javascript:void(0)" class="important_info_header"><%= t("catalog_items.show_lot.important_info_header")%></a></h4>
<div class="important_info_header text">
<p><%= t("catalog_items.show_lot.important_info") %></p>
</div><!-- /text -->
<h4><a href="javascript:void(0)" class="rules"><%= t("catalog_items.show_lot.important_info_header")%></a></h4>
<div class="rules text">
<p><%= t("catalog_items.show_lot.important_info") %></p>
</div><!-- /text -->
<h4><a href="javascript:void(0)" class="shipping"><%= t("catalog_items.show_lot.important_info_header")%></a></h4>
<div class="shipping text">
<p><%= t("catalog_items.show_lot.important_info") %></p>
</div><!-- /text -->
HTML END
$('.important_info_header').click(function() {
$('.important_info_header.text').slideToggle('slow', function() {
//ani complete.
});
});
最佳答案
您需要在选择器中获取类,
$(($this).attr('class')+'.text').slideToggle('slow', function() {
//ani complete.
});
关于javascript - Jquery javascript 使用 $this 折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13391364/