是否可以确定何时使用JavaScript呈现元素? [英] Is is possible to determine when an element has been rendered using JavaScript?

查看:119
本文介绍了是否可以确定何时使用JavaScript呈现元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有jQuery等效于执行以下操作:

Is there a jQuery equivalent to do the following:

$(document).ready(function() {

元素的

for an element:

$(a).ready(function() {

我有一个内容updatepanel和我在一些主要元素上调用jQuery UI的.button()。在updatepanel刷新后,锚点被重新渲染并失去UI样式。

I have content in an updatepanel and I am calling jQuery UI's .button() on some anchor elements. After the updatepanel refreshed, the anchors are rerendered and lose the UI styling.

我已经知道如何使用.NET AJAX的add_endrequest(处理程序)来检测ajax请求的结束,但希望使用jQuery.delegate提供更简洁的解决方案。

I already know how to detect the end of an ajax request using .NET AJAX's add_endrequest(handler), but was hoping for a neater solution using jQuery.delegate.

例如

$('body').delegate('#mybutton', 'load', (function(){  //this doesnt work... }


推荐答案

如果我理解你的要求,一种方法这是使用实时查询插件。

If I understand your requirement correctly, one way to do this is with the Live Query plug-in.


实时查询...能够在
a新元素与另一个函数
匹配时触发
a函数(回调) (回调)当元素没有
更长时间匹配

Live Query ... has the ability to fire a function (callback) when it matches a new element and another function (callback) for when an element is no longer matched

例如:

$('#someRegion a').livequery( function(){ 
  do_something();
});



更新:由于DOM更改是没有运行jQuery,不幸的是,livequery看不到它们。我之前仔细研究过这个问题,并在这个答案


更新:轮询有点难看,但如果没有其他选择,这里有一个基于轮询的解决方案,使用jQuery虚拟操作来制作livequery看到变化。你只想考虑这样的事情作为最后的手段 - 如果没有选择绑定回调方法。


Update: Since the DOM changes are not running through jQuery, unfortunately livequery doesn't see them. I mulled over this issue before and considered a polling-based solution in this answer.
Update: Polling is kind of ugly, but if there's really no other alternative, here's a polling-based solution that uses a jQuery "dummy" manipulation to make livequery "see" the change. You'd only want to consider something like this as a last resort -- if there's no option to tie into a callback method.

首先,设置livequery观看容器更新将在何处发生:

First, set up livequery watching the container where the updates will occur:

$('div#container').livequery( function(){ 
  $(this).css('color','red'); // do something
});

然后使用 setInterval(),这里包含在便利函数中:

And then use setInterval(), here wrapped in a convenience function:

function pollUpdate( $el, freq ){
  setInterval( function(){
    $el.toggleClass('dummy');
  }, freq); 
};

所以你可以:

$(document).ready( function(){
  pollUpdate( $('div#container'), 500 );
});

这是工作实例。单击按钮以添加没有jQuery的新DOM元素,您将看到它们被拾取(最终)并由livequery重新设置。不漂亮,但确实有效。

Here's a working example. Click the button to add new DOM elements without jQuery, and you'll see they get picked up (eventually) and restyled by livequery. Not pretty, but it does work.

这篇关于是否可以确定何时使用JavaScript呈现元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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