如何扩展手风琴的点击方法

时间:2012-08-09 06:10:17

标签: jquery onclick accordion extend

我需要扩展手风琴的点击方法。

我有类似的东西:

<div id="accordion">
    <h3><a href="#">
    <div id="toChange1">Section </div>1</a></h3>
<div>
    <p>
    Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
    ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
    amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
    odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
    </p>
</div>
<h3><a href="#"><div id="toChange2">Section </div>2</a></h3>
<div>
    <p>
    Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
    purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
    velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
    suscipit faucibus urna.
    </p>
</div>
<h3><a href="#"><div id="toChange3">Section </div>3</a></h3>
<div>
    <p>
    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
    </p>
    <ul>
        <li>List item one</li>
        <li>List item two</li>
        <li>List item three</li>
    </ul>
</div>

我需要在h3里面改变div的背景颜色。所以我需要打电话给:

$("#toChange1").css("backgroundColor","white");

单击该标题时。有什么建议?谢谢!

1 个答案:

答案 0 :(得分:0)

这可能会改变h3中内容的背景颜色:

$("div[id^=toChange]").on('click', function(e){  
    e.preventDefault();
    $(this).parent().parent().css("background-color","red");
});