$.fn.extend({
hasClasses: function (selectors) {
var self = this;
for (var i in selectors) {
if ($(self).hasClass(selectors[i]))
return true;
}
return false;
}
});
$('#element').hasClasses(['class1', 'class2', 'class3']);
// When your program starts up
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
// then, when you want to schedule a task
Runnable task = ....
executor.schedule(task, 5, TimeUnit.SECONDS);
// and finally, when your program wants to exit
executor.shutdown();
$.fn.extend({
hasClasses: function (selectors) {
// Setup
const _id = $(this).attr('id'); // Preserve existing id
const uuid = generateUUID(); // Create new id for query
$(this).attr('id', uuid); // Apply new id to element
// Query to find if element has any of the classes
const res = selectors.some(cls => !!$(`${uuid}.${cls}`).length);
// Rollback on id
if (!_id) $(this).removeAttr("id"); // No Id to begin with
else $(this).attr('id', _id); // Preserve old id
// Done
return res;
}
})
如果(! _ Id) $(this) . RemoveAttr (“ Id”) ;//开头没有 Id
我只是注意到,对于我的实现,我依赖于 id的异步调用。如果我需要在 hasClasses改变 id的同时通过 id 查询元素,那么我可能会导致一个问题。
Else $(this) . attr (‘ id’,_ id) ;//保留旧的 id
完成
为了规避这个问题,我们只需要添加一个唯一的 uuid属性(字面意思就是 uuid)。
返还利息;
}
这里有一个更正:
$.fn.extend({
hasClasses: function (selectors) {
// Setup
const uuid = generateUUID(); // Create new uuid to query later
$(this).attr(uuid, ""); // Apply uuid to element for query
// Query to find if element has any of the classes
const res = selectors.some(cls => !!$(`[${uuid}].${cls}`).length);
// Remove the uuid attribute
$(this).removeAttr(uuid);
// Done
return res;
}
})