2013-03-09 18 views
1

嗨昨天問過類似的問題,但在這裏不言而喻。使用jQuery來拉個桌子到iframe

首先:所有的數據是在我的域名。所有數據都在同一個域中。加載iframe在同一個域中。謝謝。

僅供參考..表名是dactable,而div名稱是tablediv(如果你希望寫一些新的東西,什麼標籤我可以理解)。

現在使用的繼承人拉表的代碼IM。

$(window).on('load', function() // wait for load event, so the iframe is fully loaded in 
{ 
    var $iframe = $('iframe'); // assuming only one? You need to target the right iframe, perahps with iframe[src="/top_list.html"] if that's your only option 

    var $contents = $iframe.contents(); 
    var $main = $contents.find('.main'); 
    var $tbl = $main.next(); // now we have the table 

    $contents.find('*').hide(); // hide everything 
    $tbl.show(); // show table and... 

    var $parent = $tbl.parent(); // get/show all parents of the tbl 

    while($parent.length) 
    { 
    $parent.show(); // show parent 
    $parent = $parent.parent(); // move up the hierarchy 
    } 
}); 

現在我還需要刪除某些列。但似乎無法得到它的工作做更多的則1

而且它是如何知道目標是什麼表?

$('table tr').each(
    function(tr_idx,el){ 
     $(el).children('td').each(
      function(td_idx,el2){ 
       //i'm removing first columns.. 
       if(td_idx == 0){ 
        el2.remove(); 
       } 
     });//inner each 
});//outer each 

謝謝

+0

您不是將表拉入iframe,而是修改生存在iframe中的表。任何它在iframe中的原因,而不僅僅是一個溢出汽車的div? – mplungjan 2013-03-09 13:31:37

+0

我在一頁上有數據。我無法在其他頁面上重複此數據或代碼。不可能。我猜在api中受到限制。所以我只能將整個頁面加載到iframe中,當我需要使用數據剝離一切我不需要的數據。這是可能的一個div? – alwayslearning 2013-03-09 13:34:58

+1

有可能使用ajax。你已經擁有你所需要的,因爲你是使用jQuery – Dogoku 2013-03-09 13:36:01

回答

1

使用jQuery的​​加載你想要的HTML到你選擇的div。你甚至可以選擇通過指定網址

$(function() { 

    $('#result').load('/top_list.html #table-id', function() { 

     $('#result table tr').each() { 
       //remove first td of each row 
       $(this).children('td').first().remove(); 
     }); 

    }); 

}); 

就這麼簡單之後提供#ID載入的HTML的一部分。它可以幫助這一切都是在同一個域太


爲了使這個多一點清晰,讓我們假設你有如下表

<table id="table-id"> 
    <tr> 
     <td>1</td> 
     <td>2</td> 
     <td>3</td> 
    </tr> 
    <tr> 
     <td>1</td> 
     <td>2</td> 
     <td>3</td> 
    </tr> 
</table> 

每一行的第一個單元被刪除,結果被放置在一個div中

<div id="result"> 
    <table id="table-id"> 
     <tr> 
      <td>2</td> 
      <td>3</td> 
     </tr> 
     <tr> 
      <td>2</td> 
      <td>3</td> 
     </tr> 
    </table> 
</div> 
+0

嘿,看起來很漂亮,我如何刪除特定的colomns,而不是第一個孩子。 – alwayslearning 2013-03-09 14:00:14

+1

您可以使用'.filter()'函數和[:eq selector](http://api.jquery.com/eq-selector/)代替'.first()'。例如'$(this).children('td').filter(':eq(1)')。remove()'將移除第二列。 – Dogoku 2013-03-09 14:03:02

+0

經歷了很多次它越來越好...頁面劑量狀態如何刪除多行。我可以做這$(this).children('td')。filter(':eq(1,2,4)')。remove() – alwayslearning 2013-03-09 14:11:15

1

試試這個代碼通過索引中刪除的行和列。

var removeRows(table, rows){ 
    // mark a row, which should be removed 
    $('tr', table).each(function(tr_idx){ 
     if(rows.indexOf(tr_idx) !== -1){ 
      // rows contains tr_idx 
      $(this).attr("remove"); 
     } 
    }); 
    // remove marked rows 
    $('tr[remove]', table).remove(); 
}, 
removeColumns(table, columns){ 
    // mark a column, which should be removed 
    $('tr', table).each(function(tr_idx){ 
     $('td', this).each(function(td_idx){ 
      if(columns.indexOf(td_idx) !== -1){ 
       // columns contains td_idx 
       $(this).attr("remove"); 
      } 
     }; 
    }); 
    // remove marked columns 
    $('td[remove]', table).remove(); 
}; 

... 
var table = $(something); 

// remove rows with indexes 1, 2 and 3 
removeRows(table, [1, 2, 3]); 

// remove columns with indexes 0 and 3 
removeColumns(table, [0, 3]); 
+0

是「-1」,不給它一個索引來刪除並使用底部列表來刪除索引? – alwayslearning 2013-03-09 14:02:36

+0

如果未找到該項目,indexOf()方法返回-1。 http://www.w3schools.com/jsref/jsref_indexof_array.asp 表達式(行。indexOf(tr_idx)=== -1)表示在「rows」數組中沒有包含索引tr_idx的行。 – Warlock 2013-03-09 14:10:09