jQuery附加函数来“加载”元素的事件 [英] jQuery attach function to 'load' event of an element

查看:58
本文介绍了jQuery附加函数来“加载”元素的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个函数附加到一个jQuery元素,只要元素添加到页面就会触发。

I want to attach a function to a jQuery element that fires whenever the element is added to the page.

我尝试了以下内容,但它没有'工作:

I've tried the following, but it didn't work:

var el = jQuery('<h1>HI HI HI</H1>');   
el.one('load', function(e) {
  window.alert('loaded');
});    
jQuery('body').append(el);

我真正想做的是保证另一个期望某些#id的jQuery函数在页面上没有失败,所以我想在页面中加载元素时调用该函数。

What I really want to do is to guarantee that another jQuery function that is expecting some #id to be at the page don't fail, so I want to call that function whenever my element is loaded in the page.

为了澄清,我将 el 元素传递给另一个库(在这种情况下,它是一个电影播放器​​但它可能是其他任何东西)并且我想知道何时 el 元素正在添加到页面,无论是我的电影播放器​​代码,它是添加元素还是其他任何元素。

To clarify, I am passing the el element to another library (in this case it's a movie player but it could be anything else) and I want to know when the el element is being added to the page, whether its my movie player code that it is adding the element or anyting else.

推荐答案


我想将一个函数附加到
jQuery元素,只要将
元素添加到页面中,该元素就会触发。

I want to attach a function to a jQuery element that fires whenever the element is added to the page.

您需要 livequery插件,它就是这样做的。最近的 live 函数类似,只是在添加元素时不会调用你。我们一直使用它 - 效果很好。

You want the livequery plugin, which does just this. The recent live function is similar, except it won't call you when the element is added. We use it all the time-- works great.

你将使用 $('h1')。livequery(function(){alert ('刚添加');});

这篇关于jQuery附加函数来“加载”元素的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆