【发布时间】:2011-05-17 09:30:41
【问题描述】:
提前感谢您的帮助!
这是我的情况:我有一组 div,其 ID 使用 PHP 在名称末尾应用了一个递增的数字。这些 div 中的每一个都是使用 PHP 动态添加的(它们是一系列常见问题解答问题,其中包含一个带有答案的隐藏 div 容器,单击问题时会向下滑动。)[Live Example][1]
页面上出现的问题数量没有限制,因为这是用于 Wordpress 主题,而我的客户希望在进行过程中添加新问题。
以下是使用 PHP 的每个 FAQ 问题的结构示例:
<?php var $faqnum = 0; $faqnum++; ?>
<div id="faqwrap<?php $faqnum; ?>">
<h4><a href="#faq<?php $faqnum; ?>" id="slidetoggle">What data is shared?</a></h4>
<div id="faqbox<?php $faqnum; ?>" class="slidebox">
<p>Data sharing is defined by the type of service:</p>
<ul class="list">
<li>Third-party access to data (Enhanced Services only is strictly controlled and determined by the SDA)</li>
<li>All members must participate in points of contact and conjunction assessment but can choose whether to participate in other services</li>
<li>Participation in a service requires the member to provide associated data<br />
</ul>
</div>
</div>
现在这是我目前在 jQuery 中的内容,它可以工作,但前提是我每次客户想要添加新问题时都添加一个新问题。
$(document).ready(function() {
$('.slidebox*').hide();
// toggles the slidebox on clicking the noted link
$("#faqwrap1 a:not(div.slidebox a)").click(function() {
$("#faqbox1.slidebox").slideToggle('normal');
$('div.slidebox:not(#faqbox1)').slideUp('normal');
return false;
});
});
我想也许可以用声明的变量做一些事情,像这样:
for (var x = 0; x < 100; x++;) {
$('#[id^=faqwrap]'+ x 'a:not(div.slidebox a)')...
}
我希望这对你来说已经足够清楚了!再次,我提前感谢您。 :)
【问题讨论】:
-
我很确定您的意思是
class="slidetoggle",而不是 HTML 中的id="slidetoggle",因为这会导致结果页面中出现重复的 ID。
标签: variables jquery-selectors dynamic auto-increment integer