如何检查特定的 jQuery UI 函数是否可用,如果不可用则使用不同的函数?

发布于 2024-09-05 16:42:31 字数 726 浏览 3 评论 0原文

我正在编写一个 jQuery 插件,我希望我的代码能够利用 jQuery UI show(effect, [options], [speed], [callback])hide(effect , [options], [speed], [callback]) 函数,允许您用漂亮的动画显示和隐藏元素。

但是,如果 jQuery UI 不可用,我还希望我的插件能够正常降级,切换回使用基本的非动画 show()hide() 标准 jQuery API 中存在的函数。

我需要专门测试显示和隐藏功能,因为即使 jQuery UI 可用,它也可能是一个自定义版本,不包含效果组件。

起初我以为我可以做类似的事情:

if(typeof myelement.show('blind', 'slow') == 'undefined')
{
    myelement.show('blind', 'slow');
}
else
{
    myelement.show();
}

但是当然这不起作用,因为即使 UI 不存在, show() 仍然是一个函数,它只是有不同的签名 - 因此 typeof 在任何情况下都会返回 object

那么,检查 jQuery UI 是否可用于我的插件的最佳方法是什么?非常感谢任何帮助!

I'm writing a jQuery plugin and I would like my code to take advantage of the jQuery UI show(effect, [options], [speed], [callback]) and hide(effect, [options], [speed], [callback]) functions, which allow you to show and hide elements with a nice animation.

However, I'd also like my plugin to degrade gracefully if jQuery UI isn't available, switching back to use the basic, non-animating show() and hide() functions present in the standard jQuery API.

I'll need to test for the show and hide functions specifically, as even if jQuery UI is available it could potentially be a custom version, without the Effects components included.

At first I thought that I might be able to do something like:

if(typeof myelement.show('blind', 'slow') == 'undefined')
{
    myelement.show('blind', 'slow');
}
else
{
    myelement.show();
}

But of course this doesn't work, as even if UI isn't present, show() is still a function, it just has a different signature—so typeof returns object in either case.

So, what's the best way for me to check if jQuery UI is available to my plugin? Any help is much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆啦不做梦 2024-09-12 16:42:38

尼克的功能发挥得非常完美;为了保持一致性,我最终在我的插件代码中将其添加为 jQuery 实用函数:

$.extend({
    hasEffect: function (effect) {
        return $.effects && $.effects[effect];
    }
});

所以现在我可以在任何想要检查是否存在特定 jQuery UI 的地方调用 $.hasEffect(effectname)影响。

Nick's function worked perfectly; for consistency, I ended up adding it as a jQuery utility function from within my plugin code:

$.extend({
    hasEffect: function (effect) {
        return $.effects && $.effects[effect];
    }
});

So now I can call $.hasEffect(effectname) anywhere I want to check for the presence of a particular jQuery UI effect.

烧了回忆取暖 2024-09-12 16:42:37

您可以检查这样的效果:

function hasEffect(effect) {
    return $.effects && $.effects[effect];
}

然后您可以在任何地方使用它:

if(hasEffect('blind')) {
  myelement.show('blind', 'slow');
} else {
  myElement.show();
}
//or, you can shorten it further:
//$.fn.show.apply(myelement, hasEffect('blind') ? ['blind','slow'] : []);​

您可以在此处查看演示,选中/取消选中左侧的 jQuery UI,然后单击顶部的“运行”以查看其运行情况。这是有效的,因为效果是这样声明的:

$.effects.blind = function(o) { ...effecty stuff... };

例如,您可以看到 此处“盲目”。我在上面的代码中检查 $.effects$.effects.effect 的原因是您只能下载 下载 jQuery UI 时的一些效果,因此这说明了这种可能性。

You can check for an effect like this:

function hasEffect(effect) {
    return $.effects && $.effects[effect];
}

Then you can use that anywhere:

if(hasEffect('blind')) {
  myelement.show('blind', 'slow');
} else {
  myElement.show();
}
//or, you can shorten it further:
//$.fn.show.apply(myelement, hasEffect('blind') ? ['blind','slow'] : []);​

You can view a demo here, check/uncheck jQuery UI on the left and click "Run" up top to see it in action. This works because effects are declared like this:

$.effects.blind = function(o) { ...effecty stuff... };

For example, you can see "blind" here. The reason I check for both $.effects and $.effects.effect in the code above is you can only download some of the effects when you download jQuery UI, so this accounts for that possibility.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文