2011-02-04 83 views
0

我想要做的操作來刪除最接近的表,當某些措辭存在於html。jquery刪除最接近tr失敗

我不能假定span類將存在那裏,因爲html是從另一端檢索的,他們可以通過添加,隨時刪除任何類來更改它。

它需要在頁面加載時完成,而不是點擊裏面的class/id事件。

有點具有挑戰性。任何想法如何實現這一目標?

在頁面加載,

jQuery來檢測是否「你好你怎麼樣」的頁面存在。如下

If exist 
    remove the whole td or tr or table for this particular. 
else 
    do nothing. 
end 

代碼:

function replaceText() 
{ 

    var debug; 
    debug= ""; 


    $("*").each(function() { 
     if($(this).children().length==0) 
    { 
//   $(this).text($(this).text().replace('Hello how are you', 'yohoo')); 

     debug= debug + $(this).val() + "<br>"; 

     if($(this).val()=="Hello how are you") 
     { 
      $(this).closest('table').remove(); 
     } 

     } 
    }); 

//alert(debug); 

} 


<div> 


<table> 

    <tr> 

    <td colspan="2"> 


     <div> 


      <table> 

      <tr> 

      <td ><span class="myclass">Hello how are you</span></td> 

      <td><a href="testing.php"></a></td> 

      </tr> 

      </table> 


     </div> 


    </td> 

    </tr> 


    <tr colspan="2"> 
     <table> 

     <tr> 

     <td><b>want to remove this</b></td> 

     </tr> 

     </table> 
    </tr> 

    <tr> 
     <td>other content 1</td> 
     <td>other content 2</td> 
    </tr> 

</table> 

</div> 
+2

我想你想`的.text()``不.VAL()`。 val是用於輸入,選擇,textareas ... – 2011-02-04 05:58:25

回答

0

您可以使用:contains檢查文本是否存在:

$(":contains('Hello how are you')").each(function(n){ 
    if ($(this).children().length == 0){ 
     $(this).closest('table').remove(); 
    } 
}); 

此代碼將通過對包含文本「你好!所有元素循環您'。如果您想要完全匹配,您還可以檢查$(this).text() == 'Hello how are you'。一旦它發現這些元素,它會檢查它們沒有任何孩子,然後如果他們通過了這個條件就移除最近的表格。

要運行該代碼在頁面加載時,在document.ready功能把它包裝:

$(document).ready(function(){ 
    ... 
});