创建 DOM 元素时运行函数

发布于 2024-08-24 05:33:48 字数 226 浏览 10 评论 0原文

我想在 DOM 中创建匹配对象时将事件绑定到特定的类和 ID。

我这样做是因为我在 ASP UpdatePanel 中有一些 jQuery 代码,这会导致 DOM 在其部分回发时重新加载。我已经使用 live() 重置了事件,但是我需要在创建元素后立即调用 2 行初始化函数。

有没有办法将其附加到 live() 或其他 jQuery 函数,或者我必须自己编写代码来执行此操作?

谢谢,艾德

I want to bind an event to a certain class and ID for when matching objects are created in the DOM.

I am doing this as I have some jQuery code in an ASP UpdatePanel, which causes the DOM to be re-loaded on its partial postback. I have reset the events with live(), however I need to call a 2 line initialisation function as soon as the elements are created.

Is there any way to attach this to live(), or some other jQuery function, or will I have to write code to do this myself?

Thanks, Ed

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

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

发布评论

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

评论(2

予囚 2024-08-31 05:33:48

如果您使用的是 UpdatePanel,您可以编写一个名为:的函数,

function pageLoad() { } //Called by ASP.Net javascript code

该函数将在每次页面最初加载时以及加载 UpdatePanel 内容时调用,或者您也可以在 UpdatePanel 返回时连接您想要运行的任何函数这样做:

$(document).ready(function() {
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(myFunction);
});
function myFunction() {
 //Do stuff with elements
}

请注意,这不会运行第一页加载,仅在 UpdatePanel 加载时运行。如果您也需要在加载时运行,只需在 document.ready 中添加对 myFunction(); 的调用即可。

If you're using an UpdatePanel you can either write a function called:

function pageLoad() { } //Called by ASP.Net javascript code

That will be called every time the page initially loads and when it loads UpdatePanel content, or alternatively you can hook up any function you want to run when an UpdatePanel comes back by doing this:

$(document).ready(function() {
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(myFunction);
});
function myFunction() {
 //Do stuff with elements
}

Note this will NOT run the first page load, only on UpdatePanel loads. Just add a call to myFunction(); in document.ready if you need it to run on load as well.

将军与妓 2024-08-31 05:33:48

每当将节点添加到 DOM 时,就会触发 DOMNodeInserted 事件。让文档正文侦听此事件,每当触发该事件时,就在插入的节点中搜索您要查找的类和 ID 选择器。

$(document.body).bind("DOMNodeInserted", function(event) {
    // element is the newly inserted node
    var element = $(event.target);
    // if this element is of interest
    if($("<selector>", element).length > 0) {
        // run the initialization code here..
    }
}

了解有关 DOM 突变事件的更多信息 此处< /a>.

A DOMNodeInserted event is fired whenever a node is added to the DOM. Have the document body listen to this event, and whenever it's fired, search the inserted node for the class and ID selectors you're looking for.

$(document.body).bind("DOMNodeInserted", function(event) {
    // element is the newly inserted node
    var element = $(event.target);
    // if this element is of interest
    if($("<selector>", element).length > 0) {
        // run the initialization code here..
    }
}

Read more about DOM mutation events here.

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