开发者

Jquery locater is looking in wrong page after reload

I have the following pages: default.aspx widgets/rss.aspx

The rss.aspx gets dynamically loaded into a div upon loading default.aspx with this code:

$.get('/Widgets/rss.aspx', { TabControlID : 385}, function(data) {
    $('#drbox385').html(data);
});

This works without a problem.

Code inside rss.aspx:

<input type="submit" value="Save" />

<div id="RSSfeed<%=TabControlID %>">

<script type="text/javascript">
  var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=<%=MyUrl%>&num=<%=MyCount %>";
  $.getJSON(gurl, function (data) {
  var feeds = data.responseData.feed;
  if (!feeds) {
      $("#RSSfeed<%=TabControlID %>").append('There was an error loading this feed.');
      }
      else
      {
          for (var i = 0; i < feeds.entries.length; i++) {
              var entry = feeds.entries[i];
              var title = entry.title;
              var link = entry.link;
              var description = entry.contentSnippet;
              var html = "<div class='stippel'><img height='10' width='10' src='/images/rss10.png' style='margin-right:2px;'><a target='_blank' href='" + link + "' class='feed_item_link' title='" + description + "'>" + title + "</a></div>";
              // this line doesn't work after reload
              $("#RSSfeed<%=TabControlID %>").append($(html));
              // end of bug
          }
      }
  })
</script>    

When you click on the Save button the configuration data is posted to an invisible iframe on default.aspx The iframe generates the following code to reload the widget with the new configuration:

$(document).ready(function(){
var str;
str = "<div style='text-align:center'><img src='/images/loading.gif' width='32' heigth='32'/></div>";
$(parent.开发者_开发问答document).find('#drbox385').html(str);
$.get('/Widgets/rss.aspx', { TabControlID : 385}, function(data) {
$(parent.document).find('#drbox385').html(data);
});
}); 

The data is loaded back into the page but this line of code in rss.aspx doesn't work:

$("#RSSfeed<%=TabControlID %>").append($(html));

Jquery is looking for ID #RSSfeed385 inside of rss.aspx instead of default.aspx

What is the correct syntax to change this behaviour?


Make the append like that.

$("#RSSfeed<%=TabControlID %>", window.top.document).append(html);

I have also add the window.top.document to tell them to search on top document and not inside the rss.aspx

http://api.jquery.com/append/

By adding the $ in front you actually search for elements to add them, just remove it to add the code exist inside the html variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜