2017-05-09 68 views
2

只是希望用jQuery轉換,是因爲我需要的風格,是由另一家公司產生的系統:jQuery的:轉換表到LS

<table class="steps"> 
     <tr> 

      <td class="steps-on-colors on step-welcome"><a href="index.cgi">Welcome</a> </td> 

      <td class="step-select-items">Catalogue </td> 

     </tr> 
</table> 

這個

<ul> 

    <li class="steps-on-colors on step-welcome"><a href="index.cgi">Welcome</a> </li> 

    <li class="step-select-items">Catalogue </li> 

</ul> 

到目前爲止,我:

$(document).ready(function() { 
    var ul = $("<ul>"); 
    $("table tr").each(function(){ 
    var li = $("<li>") 
    $("th, td", this).each(function(){ 
    var p = $("<li>").html(this.innerHTML); 
    li.append(p); 
    }); 
    ul.append(li); 
    })  
    $("table").replaceWith(ul);  
}); 

回答

0

您正在向變量p添加<li>元素,然後添加p(其中cont ain <li>)到li元素,這也是<li>

您可以直接附加到您的變量p到ul

$(document).ready(function() { 
    var ul = $("<ul>"); 
    $("table tr").each(function(){ 
     $("th, td", this).each(function(){ 
     var p = $("<li>").html(this.innerHTML); 
     ul.append(p); 
     }); 
    }) 
    $("table").replaceWith(ul); 
});