简体   繁体   中英

check if jquery ui accordion exists?

is there an easy way to tell if a jquery accordion exists on the page...i am trying to dynamically build accordion based on selection that runs through $ajax, reads values from xml, and depending on the xml file selected builds strings the make up the accordion, and finally appends it.

I think that if the accordion already exists on the page, and the user selects another file, I am having trouble destroying the accordion, clearing the html, append the new string, then creating a new accordion...

like

$("#accordion").accordion('destory').html('').append(string).accordion();

seems like if there is not already an accordion this idea breaks....thinking maybe i can just check?? thanks for any help to beginner!

I suspect that you could try checking .data() .

var isAccordion = !!$("#accordion").data("ui-accordion");

Or, by checking the ui-accordion classname using .hasClass() which is added upon initialization.

var isAccordion = $("#accordion").hasClass("ui-accordion");

You could try with .length

Count the element using .length

Or something like this

if($('#accordion').length > 0) {
    // do something
}

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