简体   繁体   English

Ajax回调刷新div

[英]Ajax callback refresh div

I implemented a Ajax callback function to update a div (announcement) in my web page. 我实现了一个Ajax回调函数来更新我的网页中的div (公告)。

It works fine, but the current issue is that the Ajax call function update the announcement div , however, after rewrite the content, the Javascript functions in this content are not load it. 它工作正常,但目前的问题是Ajax调用函数更新了公告div ,但是,在重写内容后,此内容中的Javascript函数不会加载它。 Therefore, my toggle function in table does not work after update content. 因此,更新内容后,我在表中的切换功能不起作用。

My ajax call function 我的ajax调用函数

setInterval("_checkUpdate()", 2000); //2 seconds one call.

function _checkUpdate() {
    var callback=new Object();
    callback.success=this.checkUpdateonExternalSuccess;
    callback.failure=this.checkUpdateonExternalFailure;
    YAHOO.util.Connect.asyncRequest('GET' , '/ci/ajaxCustom/ajaxCheckUpdate',callback);
};

On success, it update the content to rewrite to my Div 成功后,它会更新内容以重写我的Div

function checkUpdateonExternalSuccess (o){
    if(o.responseText!==undefined) {
        var str=o.responseText;
        if(str !== 'nocontent') {
            document.getElementById('updateContent').innerHTML=str;  (Write the new content to my div)
            location.reload(); //Reload the whole page, i do not want that
        }
    }
};

It works, it write the new content with Javascript, but the Javascript functions are not load it. 它工作,它用Javascript编写新内容,但Javascript函数不加载它。

Javascript: 使用Javascript:

$(document).ready(function() {
  //First hide all the contects except the headcontent
  $(".content").hide();
  $(".headContent").show();

  //If user click on the head title, it hide/show content
  $(".headTitle").click(function () {
     collapseHeadContent();//reset all
      var aParentTD= $(this).closest("td");
      var aHeadContent = aParentTD.find (".headContent");
      aHeadContent.toggle();
  });

  // If uses click on the title, it has toggle event for the content
  $(".title").click(function () {
      collapseContent(); //First reset all
      var aParentTD = $(this).closest("td");
      var aContent  = aParentTD.find(".content");  // Content in the same TD with Title
      aContent.toggle();
  });

});

function collapseContent() {
    //find the Head content to hide and reset of content
    $(".headContent").hide();
    $(".content").hide();
}

function collapseHeadContent() {   
    $(".content").hide();
}

HTML: HTML:

<div id="annoucementTableDiv">
     <table id="annoucementTable">
  <tbody id="tdContent">
      <tr>
   <td><span id="tdtitle"><a class="headTitle"> Forbidden: Error 403</a></span> <br />
       <span class="annfrom">PLC team  - 19/08/2010 at 10:30<br /> </span>
       <br />
       <span class="headContent"><P>Due to scheduled maintenance, HOME showed the error 403:<br/>
    This is now resolved and customers can update again as normal </P>
       </span>
   </td>
     </tr>
  </tbody>
  <tbody id="tdContent">
      <tr>
   <td>
       <span id="tdtitle">
    <a class="title">Downloading maps</a>
       </span> <img src="/euf/assets/themes/standard/images/layout/important.png" class="alert_icon" alt="alert"></img><br />
       <span class="annfrom">Sent by 2nd line  - 05/11/2009 at 15:30<br /> </span>
       <span class="content">Since this morning it has been reported that multiple customers are again receiving this error message.<br /><br />This error has been escalated to the relevant teams with the highest priority and is currently being looked into.
    An estimated time for a fix is not yet available but we will keep you updated.
       </span>
       <br />
   </td>
     </tr>
  </tbody>
     </table>
 </div>

If I do location.reload , the Javascript functions work. 如果我做location.reload ,Javascript函数可以工作。 But I do not want to refresh the page. 但我不想刷新页面。

Any ideas? 有任何想法吗?

Since you're also hiding/showing things, I recommend you change you're ready handler into a named function then call it on document.ready , like this: 既然你也在隐藏/显示东西,我建议你把你已经准备好的处理程序改成一个命名函数,然后在document.ready上调用它,就像这样:

function loadFunc() {
  $(".content").hide();
  $(".headContent").show();
}

$(function() {
  loadFunc():

  $(".headTitle").live('click', function () {
     collapseHeadContent();
     $(this).closest("td").find(".headContent").toggle();
  });

  $(".title").live('click', function () {
      collapseContent();
      $(this).closest("td").find(".content").toggle();
  });
});

This also uses .live() to set your click functions up once. 这也使用.live()来设置你的click功能一次。 Then when you do your AJAX load, you can just call loadFunc again to do hiding/showing or any other work, like this: 然后,当您执行AJAX加载时,您可以再次调用loadFunc来执行隐藏/显示或任何其他工作,如下所示:

function checkUpdateonExternalSuccess (o){
    if(o.responseText!==undefined) {
        var str=o.responseText;
        if(str !== 'nocontent') {
            document.getElementById('updateContent').innerHTML=str;
            loadFunc();
        }
    }
};

Sounds like you need the live function. 听起来你需要实时功能。 It is quite straight forward. 这很直截了当。

  $('.title').live('click', function() {
       collapseContent(); //First reset all
      var aParentTD = $(this).closest("td");
     var aContent  = aParentTD.find(".content");  // Content in the same TD with Title
     aContent.toggle();

   });

Basically adds the click handler to all title elements. 基本上将点击处理程序添加到所有标题元素。 Even if another loads the event is still associated with it. 即使另一个加载,事件仍然与它相关联。 You can use this for all elements that are loaded by AJAX during the life of the page. 您可以将此用于在页面生命周期中由AJAX加载的所有元素。 So you do the same for the head element and so on. 所以你对head元素做同样的事情,等等。

In a project I'm writing at the moment, I insert HTML and Javascript content separately. 在我正在撰写的项目中,我分别插入HTML和Javascript内容。 The HTML is inserted as usual into innerHTML. HTML像往常一样插入到innerHTML中。 And I got the javascript in a JS file which I load into my document during runtime like this: 我在JS文件中得到了javascript,我在运行时加载到我的文档中,如下所示:

var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = 'myJavascriptFile.js';
headID.appendChild(newScript);

Wrap your table related code into a function and call it once on document.ready and after you ajax-request. 将与表相关的代码包装到一个函数中,并在document.ready和ajax-request之后调用它一次。 Avoid triggering document.ready as suggested by Uoli, because it can cause unpredictable side effects as all your $(document).ready(function() { blocks will be executed again. 避免按照Uoli的建议触发document.ready,因为它会导致不可预测的副作用,因为所有$(document).ready(function(){块将再次执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM