2012-12-17 189 views
1

如何指定的元素,其中的内容是从下面的代码PAGEURL内检索时如何指定的元素?加载内容的jQuery AJAX

$(function() { 
    $("a[rel='sort']").click(function (e) { 
     //e.preventDefault(); 
     /* 
    if uncomment the above line, html5 nonsupported browers won't change the url but will display the ajax content; 
    if commented, html5 nonsupported browers will reload the page to the specified link. 
    */ 

     //get the link location that was clicked 
     pageurl = $(this).attr('href'); 

     //to get the ajax content and display in div with id 'content' 
     $.ajax({ 
      url: pageurl + '?rel=sort', 
      success: function (data) { 
       $('#content1').html(data); 
      } 
     }); 

     //to change the browser URL to 'pageurl' 
     if (pageurl != window.location) { 
      window.history.pushState({ 
       path: pageurl 
      }, '', pageurl); 
     } 
     return false; 
    }); 
}); 
+0

你在这里检索,成功:功能(数据)。它是一个json数据数组吗? – Swarne27

回答

1

你必须得到一个变量的响应,然后找到你想要的内容

试试这个

$.ajax({url:pageurl+'?rel=sort',success: function(data){ 
    var content = $(data).find("#IDofYouContent"); 
    $('#content1').html(content); 
}}); 
+0

thx这个效果很好 – john

1

使用jQuery.load()事件

详情:http://api.jquery.com/load/

$(function(){ 
    $("a[rel='sort']").click(function(e){ 
    pageurl = $(this).attr('href')+"?rel=sort #elementToBeLoadedIn"; 
    $('#content1').load(pageurl); 
    . 
    . 
} 
+0

我还可以使用$(“一[相对=‘排序’]”)。点击(函数(E){指定URL – john

+0

@phukkie是肯定..看到更新的答案 – Champ

+0

THX两个答案的工作 – john

0

您将使用load()功能,像这样:

$('#content1').load(pageurl + '?rel=sort #elementToBeLoadedIn'); 

退房的文档欲了解更多信息:http://api.jquery.com/load/