简体   繁体   中英

How to tell if an element is a jquery-ui widget?

My application is based on 100's of files. the "logic" determine which will be included in a certain display.

In many of them, I have jquery-ui-widgets (for this question, I will assume that they are all tabs ).
Some of them are customized and the others are not.
The customized tabs are set individually in the beginning of the complete output (in the html header). The others are set globally later.

HTML

<div class="some-element">...
<div class="some-other-element">...
<div class="tabs">...
<div class="tabs">...
<div class="tabs">...
<div class="tabs">...

JavaScript

$(".tabs .some-element").tabs({/*some settings*/});
$(".tabs .some-other-element").tabs({/*some other settings*/});
$(".tabs").tabs(); // no settings

My problem is: and I want the last javavscript call to exclude, the already initialized elements.

How can tell if an object is already set?

As @mhu states, you can check for the data value under the name of the widget. The widget framework also sets up a selector which matches the name of the widget itself. This will allow you to test if an item is a widget with:

if ($(testElement).is(":ui-tabs")) {
    // testElement is a ui-tabs widget
}

This is actually backed by the data attribute which can only be used to test a single item. The selector can instead be used as follows:

$(".tabs:ui-tabs").each(function() {
    // every ".tabs" which is a widget will get a call.
});

You can check the data (example below is for jQueryUI 1.9+):

if ($(".tabs").data("ui-tabs")) {
    // ui tab
}

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