简体   繁体   中英

JQuery Mobile to check if a Div is Expanded or Collapsed

I have a Div that is collapsible using attribute data-role="collapsible"

How can I check at any point in time whether the Div is in Collapsed mode or Expanded mode. I tried this but it doesn't work:

if ($("#hideshow").is(":collapsed"))
   alert("collapsed");

Please note that :visible won't work because in both states visible returns true.

This is done via CSS. When a collapsible element is collapsed, it has the class "ui-collapsible-collapsed" added. Use .hasClass() to check it

if ($("#hideshow").hasClass('ui-collapsible-collapsed')) {
    alert("collapsed");
}

Use the getter of collapsed option for this:

 if ($("#hideshow").collapsible("option", "collapsed")) { alert("collapsed"); } 

Tested and working!

 if (metingblok.collapsible( "option", "collapsed") == false ) {
//then it is closed
}

  if (metingblok.collapsible( "option", "collapsed") == true) {
//then it is open
}

Try to use:

if ($("#hideshow").attr('data-role')=="collapsible")
    alert("collapsed");

This will do the trick:

if ($("#hideshow").attr('data-role')=="collapsible") {
  console.log("collapsed");
}

Instead of console.log , enter whatever you wish as processing.

DEMO

You can also use data getter/setter function.

var isCollapsed = $("#hideshow").data('role') == "collapsible";

if (isCollapsed)
       alert("collapsed");

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