簡體   English   中英

如何在$ .get(url)中的項目上使用jquery

[英]How do i use jquery on items from $.get(url)

我想.get一個頁面,然后使用jquery查找諸如鏈接之類的元素。 如何使$('#blah')搜索獲取數據而不是頁面?

您應該能夠從返回的HTML創建dom元素,而無需將其實際添加到文檔中,然后使用jQuery方法搜索該元素:

jQuery.get('/my_url/', function(html_data) {
  // If your html_data isn't already wrapped with an HTML object, you may
  // need to wrap it like so:
  //
  // var jQueryObject = $("<div>" + html_data + "</div>");
  var jQueryObject = $(html_data);
  jQueryObject.find("a.link_class");

  // Or, as stated by gregmac below, you could just do the following:
  $("a.link_class", html_data);

  // or, if wrapping is required:
  $("a.link_class", "<div>" + html_data + "<div>");
});

要查找鏈接或特定元素(例如您的示例),可以執行以下操作:

$.get('test.html', function(data) {
   var links = $('a', data); //Use the response as the context to search in
   var blah = $('#blah', data);
});

我很確定您也將僅限於僅從本地服務器/頁面獲取鏈接。

// create blank array
var links = new Array();

// where should we $.get
var url = '/menus';

$.get(url, function(data) {

    // get anchors from the url
    links = $(data).find('a');

    // loop through all of them
    for(i=0; i<links.length; i++)
    {
        // do something (may alert a lot of links... be prepared)
        alert($(links[i]).attr('href'));
    }
});

您可以簡單地做到這一點:

$('#result').load('ajax/test.html #container');

僅顯示#container的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM