如何检查是否一个事件处理程序使用jQuery或JS存在

如何检查是否一个事件处理程序使用jQuery或JS存在

本文介绍了如何检查是否一个事件处理程序使用jQuery或JS存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查如果事件是可用的功能绑定到它。
问题是,谷歌浏览器支持视频元素出现的等待load​​edmetadata而Firefox没有。

I want to check if an event is available or not before binding a function to it.The problem is that Google Chrome support the event "loadedmetadata" in the Video element while FireFox don't.

我做了以下

$('video').bind('loadedmetadata', videoloaded);
videoloaded();

它的工作以及在Firefox,但是当我在Chrome尝试,被执行的函数的两倍(这是合乎逻辑)。我想检查是否等待load​​edmetadata 事件处理程序的存在或不运行的功能只有一次在每个浏览器。

It worked well in Firefox but when I tried in Chrome, the function was executed twice (which is logical). I want to check if loadedmetadata event handler exists or not to run the function only one time in each browser.

如果这种可能性不存在,任何一个聪明的工作围绕这个?

If such possibility doesn't exist, any intelligent work around for this?

推荐答案

检查 $ video.data(事件)如果这个对象包含您的活动,因为你使用 .bi​​nd 此元素的所有事件都将存储在该对象。

Check the $video.data("events") if this object contains your event, since you are using .bind all events of this element will be stored in this object.

var $video = $("#video");
var $ve = $video.data("events");

// checking if a `loadedmetadata` object exists in `data("events")`
if ($ve != null && typeof($ve.loadedmetadata) !== undefined)
{
    // has loadedmetadata event
}

上的jsfiddle 展开工作

Full working example on jsFiddle

这篇关于如何检查是否一个事件处理程序使用jQuery或JS存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:15