Jquery Event onChange for Div

我的未来我决定 提交于 2019-12-08 05:50:36

问题


I have a page with the following two divs:

<div id="searchResults">
</div>

<div class="postSearchOptions" style="display: none;">
</div>

Is there any way that I can make the "postSearchOptions" div appear when the "searchResults" div is updated by an AJAX call? I don't control the AJAX calls and I want to detect any change in the "searchResults" div.

I tried writing the following JQuery code, but then realized that it requires Jquery 1.4 and I only have 1.3:

$("#searchResults").live("change", function() {
    $(".postSearchOptions").css("display", "inline");
});

Is there any way to catch the event of the searchResults div changing using either standard JavaScript or Jquery 1.3? Thanks!


回答1:


I don't think the onchange event will fire if you are programatically changing the innerHTML. Why don't you just show the Post Search options upon receiving those change i.e. why don't you include it as the last line in your ajax success method.

HTH




回答2:


If the AJAX calls are made using jQuery, you could call handle the global ajaxComplete event and run your code there.




回答3:


You could use setInterval to watch it, but as others have said it would be nicer to detect the change in the ajax callback. Here's an sketch of what a plugin would look like to "watch" a node, like you're trying to do with live:

jQuery.fn.watch = function() {
  this.each(function() {
    var original = $(this).html();
    setInterval(function() {
       var newHtml = $(this).html();
       if (newHtml != original) {
         $(this).trigger('change');
         original = newHtml;
       }

    }, 500);

  } );
}



回答4:


to working, do....

jQuery.fn.watch = function() {
      this.each(function() {
        var obj = $(this);
        var original = $(this).html();
        setInterval(function() {
           var newHtml = $(obj).html();
           if (newHtml != original) {   
            $(obj).trigger('change');
            original = newHtml;
           }

        }, 500);

       } );
}


来源:https://stackoverflow.com/questions/2638228/jquery-event-onchange-for-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!