重新加载$(document).ready(function()重新加载Ajax页面后

编程入门 行业动态 更新时间:2024-10-25 02:30:44
本文介绍了重新加载$(document).ready(function()重新加载Ajax页面后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个通用的js文件 在ajax请求中重新加载html页面后,我无法访问此文件中的函数, $(document).ready(function()之间的常见JS函数) 如何访问它们并触发通用文件中的功能 示例:

i have a common js file after I reload html page in ajax request , I cant access the functions in this file , the common JS functions between $(document).ready(function() How can Access them and fire the functions in common file Example :

COMMON JS:

COMMON JS :

$(document).ready(function() { $(".agree_btn").click(function(){ alert(123); }); });

phtml 页面中的功能

$('.loadMoreAnswers').live('click', function(event) { var location_id = $(this).attr('location_id'); var counter= $(this).attr('counter'); $('#loadingAnswer').show(); $.ajax({ type: 'POST', url: '/daleel/loadmore', data: 'location_id='+location_id+'&part='+'answers'+'&answerCounter='+counter, //with the page number as a parameter success: function(msg){ if(msg.length!=0) //if no errors { $(this).parent().load("view") $('#loadingAnswer').remove(); counter+=5; $('#profile-page-answer').append(msg); } else $("#loadingAnswer").remove(); }, dataType: 'html' }); });

其呈现HTML的方式如下:

its render the HTML like This :

<a agreed="no" agreed-content-id="63066" class="agree_btn" id="agree-a63066"> Agree </a>

但是当我单击此链接时 它不会在Common JS文件中运行该功能

But when i click on this Link it doesnt run the function in the Common JS file

推荐答案

在ajax成功中重新绑定click事件处理程序

rebind the click event handler in the ajax success

success: function(msg){ //your code $(".agree_btn").bind('click'); }

,或者对于

$(document).delegate(".agree_btn",'click',function(e){ //your code });

或者您正在使用jQuery 1.7+版本,请使用 on 方法

or of you are using jQuery version 1.7+ use on method

$(document).on("click",".agree_btn",function(e){ //your code });

请勿使用.live已弃用的 docs

do not use .live its deprecated docs

从jQuery 1.7开始,不推荐使用.live()方法.使用.on()来 附加事件处理程序.较旧版本的jQuery用户应使用 .delegate()优先于.live().

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

更多推荐

重新加载$(document).ready(function()重新加载Ajax页面后

本文发布于:2023-11-01 19:58:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   页面   document   ready   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!