Jquery:将表转换为 ls
Jquery: Converting Tables into ls
只是想用 jQuery 转换它,因为我需要设计由另一家公司生成的系统:
<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);
});
您正在向变量 p 添加一个 <li>
元素,然后将 p(其中包含一个 <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);
});
只是想用 jQuery 转换它,因为我需要设计由另一家公司生成的系统:
<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);
});
您正在向变量 p 添加一个 <li>
元素,然后将 p(其中包含一个 <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);
});