代码之家  ›  专栏  ›  技术社区  ›  Josh Andreas Rehm

Javascript可以访问Ajax文本/html响应的DOM吗?

  •  7
  • Josh Andreas Rehm  · 技术社区  · 17 年前

    7 回复  |  直到 17 年前
        1
  •  10
  •   jon skulski    17 年前

    Jquery(或其他库??)基本上会为您完成所有这些。我强烈建议研究一下这个问题,而不是重新发明轮子。

    对于查询,它可能会像这样:

    // Make a call to url, with data 
    $.ajax(
      { url: $url,
        data: $data, 
        dataType: 'xml',
        callback: function(returnData) { 
          // Jquery find <mytag attribute="foo">...</mytag> and store it in mydata
          var mydata = $(returnData).find('mytag[attribute=foo]');
          // Insert into current page to somewhere with class="after-me"
          $('.after-me').html(mydata);
       }
    });
    

    我可能语法不对。但以下是文档: http://docs.jquery.com/Ajax/jQuery.ajax#options

        2
  •  1
  •   geowa4    17 年前

    Here 您将找到如何解析XML响应的资源。

    innerHTML ),您应该能够调用您习惯的标准DOM遍历函数。

        3
  •  1
  •   skrat    17 年前

    不要担心有效性,因为您是使用javascript动态执行此操作的,您可以用响应数据填充隐藏元素的innerHTML,然后使用您想要的任何DOM/jQuery函数

        4
  •  0
  •   Tolgahan Albayrak    17 年前

    <div id="content">some html</div>
    

    content.innerHTML = "foo";
    

    在ajax响应中使用eval函数

    function onXmlHttpAnswer()
    {
       if(xmlHttp.readyState==4 && xmlHttp.status==200)eval(xmlHttp.responseText);
    }
    
        5
  •  0
  •   betabennett    13 年前

    为了使用jQuery只提取通过$.load()加载的HTML文档的一部分,请执行以下操作:

    $('#dynamic').load('content.html #desiredElement');
    

        6
  •  0
  •   Vinit Kadkol    8 年前

    试试这个。这对我奏效了。

    $.ajax({
        url: 'http://www.example.com',
        type: "GET",
        crossDomain: true,
        dataType: 'html',
        success: function (responseHTMLData) {   
            $($(responseHTMLData).find('.yourClassName')).each(function() {
                console.log($(this).attr('href'));
            });
        }
    });
    
        7
  •  0
  •   dschu    8 年前
    $.ajax({
      type: "POST",
      url : "yourURL"
      data : $("#yourFormID").serialize(),
      async: true,
      success : function(resp) {
        // resp contains data in html form you want to find data in
        // <span class="error">some Error</span>
        var errorData = $(resp).find('span.error').text();   
        // errorData would have value "some Error"             
      }
    });