2013-05-13 92 views
1

我再次遇到了一個問題(我想)。我想寫一個儘可能通用的函數來獲取一個網頁,刪除一些它的代碼之前,我的內容設置爲一個佔位符(<DIV>嵌套表格,刪除包含模式的內表

下面加載頁面的示例代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>testtesttest</title> 
</head> 

<script type="text/javascript" src="/AJAX/jquery-1.6.4.min.js"></script> 
<script type='text/javascript' src='/util.js'></script> 

<body> 
<div class='contentcontainer'> 
<hr> 

<div class='contentitem' id='restart' style="margin-top: 100px; margin-left: 100px; margin-bottom: 100px; display:block;"> 
    <table> 
     <tr> 
      <td><img src='/images/warn__xl.gif'></td> 
      <td><p style="font-size: 1.2em; font-weight: bold; color:#02A3C1;">Loading: please wait...</p></td> 
     </tr> 
    </table> 
</div> 

<div class='contentitem' id='ready' style="margin-top: 100px; margin-left: 100px; margin-bottom: 100px; display:none;"> 
     <div id="content"> 
     </div> 
</FORM> 
</div> 


</div> 

    <script type='text/javascript'> 
     var content = $("#content"); 
     //var content = $("#ready"); 

    //hide loading bar 
    function hideLoading(){ 
     loading.fadeTo(1000, 0); 
    }; 

    $(document).ready(function(){ 
     content.load('/AJAX/test/cfg_3.html div .contentitem', function(content) { 
     //$(this).find('table:not(:first-child)').css("font-style", "italic"); //just need to replace the css by remove() to remove what is italic. 
     $(this).find('table:eq(2)').css("font-style", "italic"); //just need to replace the css by remove() to remove what is italic. 

     /*$(this).find('table:contains("Content Based"):first-child').each(function(i){ 
        //.css("display", "none"); 
        // tabelle[i] = $(this).attr("onChange"); 
       //$(this).attr("id",i) 
       alert(i); 
      });*/ 

     $(this).find('tbody').each(function(){ 
       //$("tr").children("td").find('table:contains("Content Based"):first-child').each(function() {$(this).css("font-style", "normal");}); 
       $("tr").children("td").find('table:contains("Content Based")').each(function() {$(this).css("background-color", "yellow");}); 
       //$("tr").children("td").find('table:contains("Content Based"):first-child').each(function() {$(this).remove();}); 
      }); 
     document.getElementById("restart").style.display = "none"; 
     document.getElementById("ready").style.display = "block"; 
     //alert('Load was performed.'); 
    }); 
    }); 

    //intelligent version of submitform: will only refresh what was needed: 
    (function() { 
    // 
    var proxied = submitForm; 
    submitForm = function(theForm, actionNo, flags, primkey, confirmMsg, anchor, eval_code, customP) { 
    //potential issue with the switch language... I think actionNo = 0 means reload. 
    //alert('gotcha'); 
    // Function submit a form 
     $.ajax({ 
      //type: theForm.attr('method'), 
      //type: get, 
      //url: theForm.attr('action'), 
      //content.load('/AJAX/test/cfg_3.html div .contentitem', function(content) { 
      url: "/AJAX/test/cfg_3.html", 
      //data: theForm.serialize(), 
      success: function (data) { 
       alert('ok'); 
       var dataHTML = $(data).html().filter("div .contentitem"); 
       $(dataHTML).find('table:eq(2)').css("font-style", "bold"); //just need to replace the css by remove() to remove what is italic. 
       content.html(dataHTML); 
      } 
     }); 

    } 
    return false; 
    })(); 

</script> 

</body> 
</html> 

而加載頁面的摘錄:

<HTML> 
<HEADER> 
    <script type='text/javascript' src='/util.js'></script> 
</HEADER> 
<BODY> 
    <div class='contentcontainer'> 
     <hr> 
     <div class='contentitem'> 
      <table cellspacing='0' cellpadding='0'> 
       <tr> 
        <td class='icon' valign='top' width='100px'><img src='/images/webf__xl.gif' alt='Parental Control'></td> 
        <td class='data' valign='top'> 
         <table cellspacing='0' cellpadding='0'> 
          <tr> 
           <td align='left'><span class='itemtitle'>Parental Control</span></td> 
           <td align='right'></td> 
          </tr> 
          <tr> 
           <td colspan='2'> 
            <p>This page summarizes the configuration of your Thomson Gateway regarding web site filtering. </p> 
            <form name='SiteFilterCfg' action='/cgi/b/sfltr/cfg/' method='post'> 
             <br> 
             <table cellspacing='0' cellpadding='0' width='100%'> 
              <tr> 
               <td width='40' valign='top'><img src='/images/bull__md.gif' alt=''></td> 
               <td valign='top'> 
                <span class='blocktitle'>Title</span><br> 
                <table width='100%' class='datatable' cellspacing='0' cellpadding='0'> 
                 <tr> 
                  <td></td> 
                  <td width='30px'></td> 
                  <td width='220px'></td> 
                  <td width='50px'></td> 
                 </tr> 
                 <tr> 
                  <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
                 </tr> 
                 <tr> 
                  <td width='170'>Use Address Based Filter:</td> 
                  <td colspan='3'><input type='checkbox' name='30' value='1' ></td> 
                 </tr> 
                 <tr> 
                  <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
                 </tr> 
                 <tr> 
                  <td width='170'>Action:</td> 
                  <td colspan='3'> 
                   <select name='31' style='width: 270px'> 
                    <option value='0' >Block</option> 
                    <option value='1' selected>Allow</option> 
                   </select> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
                 </tr> 
                 <tr> 
                  <td colspan='4' align='right'> 
                   <input type='button' name='25' value='Apply' onClick='submitForm(document.SiteFilterCfg,25,0,"","",0,"")' > 
                   <input type='button' name='26' value='Cancel' onClick='submitForm(document.SiteFilterCfg,26,0,"","",0,"")' > 
                  </td> 
                 </tr> 
                </table> 
                <p>Some text. </p> 
                <table class='edittable' width='100%' cellspacing='0' cellpadding='0' border='0'> 
                 <tr> 
                  <td colspan='5' class='black'><img src='/images/spacer.gif' border='0' width='1' height='1' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <td colspan='5'><img src='/images/spacer.gif' border='0' width='1' height='3' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <th align='left'>Web Site</th> 
                  <th align='left'>Action</th> 
                  <th align='left'>Redirect</th> 
                  <th colspan='2'></th> 
                 </tr> 
                 <tr> 
                  <td colspan='5'><img src='/images/spacer.gif' border='0' width='1' height='3' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <td colspan='5' class='black'><img src='/images/spacer.gif' border='0' width='1' height='1' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <td class='evenrow' colspan='5'><img src='/images/spacer.gif' border='0' width='1' height='10' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <td class='evenrow' colspan='5' align='center'><i>No filter rules defined.</i></td> 
                 </tr> 
                 <tr> 
                  <td class='evenrow' colspan='5'><img src='/images/spacer.gif' border='0' width='1' height='10' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <td colspan='5' class='black'><img src='/images/spacer.gif' border='0' width='1' height='1' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <td class='oddrow' colspan='5'><img src='/images/spacer.gif' border='0' width='1' height='3' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <td class='oddrow'><input type='text' name='32' maxlength='80' onkeypress='return noenter(event)' style='width: 60 '></td> 
                  <td class='oddrow'> 
                   <select name='33' style='width: 60 '> 
                    <option value='0' selected>Block</option> 
                    <option value='1' >Allow</option> 
                    <option value='2' >Redirect</option> 
                   </select> 
                  </td> 
                  <td class='oddrow'><input type='text' name='34' maxlength='80' onkeypress='return noenter(event)' style='width: 60 '></td> 
                  <td class='oddrow' colspan='2' align='right' valign='middle'><input type='button' name='19' value='Add' onClick='submitForm(document.SiteFilterCfg,19,1,"","",0,"")' ></td> 
                 </tr> 
                 <tr> 
                  <td class='oddrow' colspan='5'><img src='/images/spacer.gif' border='0' width='1' height='3' alt=''><br></td> 
                 </tr> 
                 <tr> 
                  <td colspan='5' class='black'><img src='/images/spacer.gif' border='0' width='1' height='1' alt=''><br></td> 
                 </tr> 
                </table> 
               </td> 
              </tr> 
             </table> 
            </form> 
            <br> 
            <table cellspacing='0' cellpadding='0' width='100%'> 
             <tr> 
              <td width='40' valign='top'><img src='/images/bull__md.gif' alt=''></td> 
              <td valign='top'> 
               <span class='blocktitle'>Content Based Filtering</span><br> 
               <form name='ContentFilterCfg' action='/cgi/b/sfltr/content/cfg/' method='post'> 
                <table width='100%' class='datatable' cellspacing='0' cellpadding='0'> 
                 <tr> 
                  <td></td> 
                  <td width='30px'></td> 
                  <td width='220px'></td> 
                  <td width='50px'></td> 
                 </tr> 
                 <tr> 
                  <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
                 </tr> 
                 <tr> 
                  <td width='170'>License Type:</td> 
                  <td colspan='3'>None</td> 
                 </tr> 
                 <tr> 
                  <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
                 </tr> 
                 <tr> 
                  <td width='170'>License Expiration:</td> 
                  <td colspan='3'>Unknown (server not yet contacted)</td> 
                 </tr> 
                 <tr> 
                  <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
                 </tr> 
                 <tr> 
                  <td width='170'>Use Content Based Filter:</td> 
                  <td colspan='3'><input type='checkbox' name='30' value='1' ></td> 
                 </tr> 
                 <tr> 
                  <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
                 </tr> 
                 <tr> 
                  <td width='170'>Action for uncategorised sites:</td> 
                  <td colspan='3'> 
                   <select name='31' style='width: 270px'> 
                    <option value='0' selected>Block</option> 
                    <option value='1' >Allow</option> 
                   </select> 
                  </td> 
                 </tr> 
                 <tr> 
                  <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
                 </tr> 
                 <tr> 
                  <td></td> 
                  <td></td> 
                  <td valign='bottom' colspan='2'>Block all categorized websites</td> 
                 </tr> 
                </table> 
               </form> 
             <tr> 
              <td height='7' colspan='4'><img src='/images/spacer.gif' width='1' height='7' border='0' alt=''></td> 
             </tr> 
             <tr> 
              <td colspan='4' align='right'> 
               <input type='button' name='10' value='Apply' onClick='submitForm(document.ContentFilterCfg,10,0,"","",0,"")' > 
               <input type='button' name='11' value='Cancel' onClick='submitForm(document.ContentFilterCfg,11,0,"","",0,"")' > 
              </td> 
             </tr> 
             </td></tr> 
            </table> 
           </td> 
          </tr> 
         </table> 
        </td> 
       </tr> 
      </table> 
     </div> 
    </div> 
</BODY> 
</HTML> 

我想<span class='blocktitle'>Content Based Filtering</span>後刪除「第二」表。在這個函數中,我不是在做remove(),而是用黃色着色。看起來整個頁面都是黃色的... 我的感覺是「這個」不在正確的範圍內。

任何想法如何我可以得到最內在的表和只有一個,不匹配所有包含表?

編輯:

感謝CLAV,我發現了一個辦法做到這一點在文件準備好功能。現在,我試圖讓「submitForm」更加巧妙,並刷新div而不是重新加載頁面。

我想我只需要複製粘貼代碼,使其工作,但它並沒有直接的工作...

這裏說的文件準備工作的代碼的一些摘錄:

$(document).ready(function(){ 
content.load('/AJAX/test/cfg_3.html div .contentitem', function(content) { 
    $(this).find('tbody').each(function(){ 
    $("span:contains('Content Based Filtering')").parent("td < tr < tbody < table").css("background-color", "yellow"); 
    }); 
document.getElementById("restart").style.display = "none"; 
document.getElementById("ready").style.display = "block"; 
}); 
}); 

不過,如果我做同樣的submitform(我使用Ajax提交):

(function() { 
    // 
    var proxied = submitForm; 
    submitForm = function(theForm, actionNo, flags, primkey, confirmMsg, anchor, eval_code, customP) { 
     $.ajax({ 
      url: "/AJAX/test/cfg_3.html", 
      // for test I do a get 
     datatype: "html" 
    }) 
    .done(function (html) { 
       $(html).find('td').each(function(){ 
      $("span:contains('Content Based Filtering')").parent("td").parent("tr").parent("tbody").parent("table").css("background-color", "yellow"); 
      }); 
       content.html(html); 
    }) 
    .fail(function (html) { 
     window.location.reload(); 
    }); 
     } 
    return false; 
    })(); 

它不工作。 我的理解是,這是因爲cfg3.html的源代碼不包含<tbody>標記。所以我改變並裝載在佔位符的HTML,然後修改它(我放棄了嘗試另一個層次,因爲它失敗):

(function() { 
    // 
    var proxied = submitForm; 
    submitForm = function(theForm, actionNo, flags, primkey, confirmMsg, anchor, eval_code, customP) { 
     $.ajax({ 
      url: "/AJAX/test/cfg_3.html", 
      // for test I do a get 
     datatype: "html" 
    }) 
    .done(function (html) { 
       content.html(html); 
      content.find('tbody').each(function(){ 
      $("span:contains('Content Based Filtering')").parent("td").parent("tr").parent("tbody").parent("table").css("background-color", "yellow"); 
      }); 
      }) 
    .fail(function (html) { 
     window.location.reload(); 
    }); 
     } 
    return false; 
    })(); 

我的問題是,這在Firefox,但不是在IE ...我不明白爲什麼,因爲基本上我在submitForm中做的和我在document.ready中做的一樣。

有什麼想法?

回答

0

您將黃色着色爲表格單元的代碼循環遍歷文檔上的所有表體,並且該循環中的行選擇器沒有限定在任何特定表格中,因此您真的在循環遍歷每個表格行爲頁面上的每個表格記錄一次。如果我正確理解你,你想在formtable與類datatable包含文字span後立即「基於內容的過濾」,你應該能夠只是這樣做:

$("span:contains('Content Based Filtering')").next("form").children("table.datatable").css("background-color", "yellow"); 
+0

不幸的是,似乎這種關係不是那麼容易... 我發現這是工作,雖然不是很好恕我直言: '$(「span:contains('Content Based Filtering')」)。parent(「td 2013-05-13 19:10:09

+0

你可以在父表上放一個類嗎? – clav 2013-05-13 19:24:39

+0

不幸的不是。我目前正在測試我的電腦,但實際上這是在傳統SW(CGI生成的頁面)之上構建一個新的界面。 – 2013-05-14 09:10:23