jquery - 将 jQuery 方法绑定(bind)到通过 Ajax 调用加载的 Html

标签 jquery html ajax

我有从 ajax 调用加载的内容,但我想将自定义方法(而不是事件)绑定(bind)到 dom 元素以实现某些功能。

我在网上找到的大多数解决方案都是将事件绑定(bind)到页面,例如

$(".example").ajax({
  'success': function(data) {
     something()
  }
})

我想实现“加载内容”之类的目标,因为那里没有事件..

$('.post').live('load', function() {
       .....
    });

最佳答案

考虑.ajaxComplete

http://api.jquery.com/ajaxComplete/

例如:

// Normally you have an anonymous function. Make it a named function.
function myInitialize(scope) {
    $('.button', scope).button();
}

// Call it in document ready to initialize stuff that loaded in the page.
$(myInitialize(null));

// Call it again in .ajaxComplete
// 'this' is the div that loaded or has new content.
$('*').ajaxComplete(myInitialize(this));

关于jquery - 将 jQuery 方法绑定(bind)到通过 Ajax 调用加载的 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4797199/

相关文章:

jquery - 计算切换前后jquery的内容区域

asp.net - 如何在 mvc .net Web 应用程序中从 jquery(在自定义 View 页面中)调用 Controller 后操作

html - 如何在列表项的中心制作文本?

jquery - 如何使用AJAX实现页面实时更新?

javascript - 如果验证错误,则不允许提交 ajax url

jquery - 自定义按钮从 JQuery Datepicker 中消失

jquery - 通过 jquery 设置值后 Bootstrap Validator 仍然显示错误消息

javascript - 如何在javascript标签中编写php代码

php - 使用 PHP 解析 HTML 页面以从下拉列表中获取选定的值。是否可以?

Javascript click事件触发两次