尝试使用jQuery创建可折叠/可扩展的div,但它根本不适合我…每个h3应该扩展/折叠它下面的div,我不确定为什么这不起作用…当然,是一个重度嵌套的div,但我认为下面的脚本会找到uforms类,无论页面在加载时有多少其他标记,然后执行它应该做的事情……
这是jquery:
$(document).ready(function () { $('div.uforms:eq(1)> div:gt(-1)').hide(); $('div.uforms:eq(1)> h3').click(function() { $(this).next('div:hidden').slideDown('fast').siblings('div:visible').slideUp('fast'); }); });
而且,标记(减去实际位于< div>< / div>中的所有东西,因为它是很多形式的东西……)
<div class="uforms"> <h3>Heading</h3> <div></div> <h3>Heading</h3> <div></div> <h3>Heading</h3> <div></div> </div>
解决方法
我认为
this就是你想要实现的目标
我强烈推荐jQueryUI框架.
<!doctype html> <html lang="en"> <head> <Meta charset="utf-8"> <Meta name="viewport" content="width=device-width,initial-scale=1"> <title>jQuery UI Accordion - Default functionality</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> $( function() { $( "#accordion" ).accordion(); } ); </script> </head> <body> <div id="accordion"> <h3>Section 1</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>Section 2</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>Section 3</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>Section 4</h3> <div> <p> Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est. </p> <p> Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra,per inceptos himenaeos. </p> </div> </div> </body> </html>