2011-05-28 70 views
1

我有一个包含以下代码的多个实例的页面:jQuery的遍历问题

<div class="company"> 
    <a class="toggle" id="<%= "tgl#{company.id}" -%>">...</a> 
    <p>...other stuff...</p> 
    <div class="clear"></div> 
    <div class="expand_me companyExpand" id="">... 

这段代码显示每个“板”的开始。当用户点击.toggle时,我需要.expand_me使用Jquery切换打开。

这是我的代码到目前为止 - 它不工作,我认为这是由于遍历问题。

$(".toggle").click(function() { 
    $(this).parent().children(".expand_me").toggle("blind", {}, 500, callback); 
}); 

我该如何解决这个问题?

+0

您例如不显示在'了'标签关闭......是'p'并列入了''或同级别的div? – 2011-05-28 10:13:52

+0

我可能不是遍历问题,但你如何调用'toggle'。看看它所需的参数:http://api.jquery.com/toggle/也不是没有'href'属性的链接不会被渲染为链接。这工作:http://jsfiddle.net/csgHQ/ – 2011-05-28 10:16:59

+1

@Felix,OP是使用jQuery的UI扩展版本http://jqueryui.com/demos/toggle/ – 2011-05-28 10:18:04

回答

0

最安全的办法是

$(".toggle").click(function() { 
    $(this).closest('.company').find('.expand_me').toggle("blind", {}, 500, callback); 
    return false; // to avoid following the actual link 
}); 

另外请注意,您必须使用该定义的方法callback。 (或使用你想要的功能的名称,而不是
我说这个以防万一你从jquery例子复制/粘贴代码。

http://jsfiddle.net/gaby/duAue/

1
$(".toggle").click(function() { 
    $(this).siblings(".expand_me").slideToggle(500, callback); 
}); 
+0

盖比的版本适合我。谢谢你,我很欣赏它的简洁。 +1。 – sscirrus 2011-05-28 10:22:05