简体   繁体   中英

Expand/Collapse all list elements in jQuery ui mobile

I'm trying to place a button on a webpage that uses jquery ui mobile to structure collapsible lists to expand/collapse all lists. However, I'm fairly new to jquery and javascript coding so I've run into a few problems. I've placed the button in but the code for it is not responding upon clicking it. How would I go about correcting this code so it successfully expands/collapses?

The code for the button is here:

<script type="text/javascript" src="js/jquery-1.11.2.min.js">

$('#collapsible1').click("click", showHide);

var collapsible = $('.collapsible');
collapsible.hide();

function showHide() {
    if(collapsible.is(':hidden'))
        collapsible.slideDown();
    else
        collapsible.slideUp();
}

</script>

The button is initialized as such:

<button id="collapsible1">Show/Hide</button>

The template I'm testing it in is here: http://jsfiddle.net/TLittler/9ndzm3cc/5/

The collapsible widget has expand and collapse methods:

http://api.jquerymobile.com/collapsible/#method-expand

So you can expand all collapsibles like this:

$('[data-role="collapsible"]').collapsible( "expand" );

and collapse them like this:

$('[data-role="collapsible"]').collapsible( "collapse" );

Updated FIDDLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM