2011-11-22 40 views
0

我有負載( 'file.html #target .subTarget')不工作

我試圖

$(destino).load('all.html #reservas_1',function(){ 
       console.log('cargado!'); 
       alert($(destino).html()); //the alerts shows HTML 
}).show(); 

,它工作正常,

但進入的HTML代碼包由至極有一個顯示器沒有,所以我想要

$(destino).load('all.html #reservas_1 .content',function(){ 
       console.log('cargado!'); 
       alert($(destino).html()); //the alert shows black string 
     }).show(); 

任何想法爲什麼?是因爲我使用一個類作爲選擇器或者是我無法加載一個子目標?

+1

你能顯示來自all.html的內容嗎? – Thys

+0

您的第一段代碼是否提醒具有「.content」類的元素? –

+0

@AbdullahBattal是的它確實 –

回答

1

也許你需要加載後更新HTML:

$(destino).load('all.html #reservas_1',function(){ 
      //$(destino).find('.content').unwrap(); 
      $(destino).html($(destino).find('.content').html()); 
      alert($(destino).html()); //the alert shows black string 
    }).show(); 

看起來它允許通過ID監守這意味着它應該與被請求的頁面上的ID只有一個元素只使用選擇(通過HTML規範它不應該是2個具有相同ID的元素)。因此,如果您將使用更復雜的選擇器,它可以返回元素數組,而jQuery不知道如何合併所有元素。

jQuery使用瀏覽器的.innerHTML屬性來解析檢索到的文檔並將其插入到當前文檔中。

如果您有元素數組,您無法以這種方式獲取html。

+0

啊哈是的,但.load會加載在destino內容與內容分區.. –

+1

哦..反正,我已經修復了例子。你現在只需要解開元素 – Samich

+0

;該句子不會刪除.content包裝,而是$(destino).html($(destino).find('。content')。html());確實,你認爲更好的解決方案? –