在jQuery中将所有<td>content</td>替换为<li>content</li>
replace all <td>content</td> with <li>content</li> in jQuery
如何从我的HTML代码中删除所有标签:table
,tbody
,tr
?最后我想填满所有
<td>
<input id="selectOne" type="radio" value="1" name="selectOneRadio">
</td>
跟
<li>
<input id="selectOne" type="radio" value="1" name="selectOneRadio">
</li>
我需要一个jQuery函数。请帮我:)
<table>
<tbody>
<tr>
<td>
<input id="selectOne" type="radio" value="1" name="selectOneRadio">
<label for="selectOne">
</td>
</tr>
</tbody>
</table>
从评论...
label
元素应保留。我尝试过:
$('table td').each(function () {
$(this).replaceWith(function () {
return $('<li>' + this.innerHTML + '</li>')
})
});
然后
jQuery.fn.unwrap = function (el) {
return this.each(function () {
$(this.childNodes).appendTo(this.parentNode);
});
};
$('tr, table, tbody').unwrap().remove();
但它对我不起作用。
创建一个包含 document.createElement
的列表。然后遍历表的rows[]
数组,对于每个数组,获取cells[]
数组并执行您需要的操作,以便将其放入列表项中(可能document.createElement
创建列表项,然后将单元格的innerHTML
复制到其中)。
完成后,使用 insertBefore
将列表放在表所在的位置,最后使用 removeChild
删除表。
上述实施:
function replaceTableWithList(tableid) {
var table = document.getElementById(tableid),
rows = table.rows, cells, list = document.createElement('ul'),
len = rows.length, i, item;
for( i=0; i<len, i++) {
item = list.appendChild(document.createElement('li'));
cells = rows[i].cells;
while(cells[0].firstChild) item.appendChild(cells[0].firstChild);
// this only gets the contents of the first cell in a row
// to get the whole row will need another loop
}
table.parentNode.replaceChild(list,table);
}
我会使用解包/包装功能:
$('table').contents().unwrap();
$('tr').contents().unwrap();
$('td').contents().unwrap().wrap('<li/>');
我会把你的桌子包装在某种容器中:
<div id="content">
<table>
<tbody>
<tr>
<td>
<input type="radio" />
</td>
</tr>
<tr>
<td>
<input type="radio" />
</td>
</tr>
<tr>
<td>
<input type="radio" />
</td>
</tr>
</tbody>
</table>
</div>
然后使用 jQuery 并执行以下操作:
// Hide the table
$('#content table').hide();
// Append a ul to the content div
$('<ul />').appendTo('#content');
// For each td, wrap children in an li and append to the ul
$('#content table td').each(function (index, element) {
$(element).children().appendTo('#content ul').wrap('<li />');
});
相关文章:
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- 更改li标记文本的Javascript
- 完全可链接的li元素
- 使用jquery动态创建ul-li
- tiltSlider播放/暂停视频元素(如果li有当前类)
- 使用下拉列表筛选列表(ul>li)
- 在bootstrap中显示隐藏特定的li
- AngularJS Navigation (li ng-repeat, create header li), DOM
- 使用Jquery在li内部获取具有特定文本的锚点
- 隐藏/显示包含单词的ul li项目.但只能入住李的子女
- 如何将li类更改为li id's在Jquery中
- @Url.Content(“~”)不适用于localhost
- 节点,express应用程序返回text/javascript响应头(Content-Type),尽管设置了applic
- ul-li专属功能
- 在LI点击时更改类
- 如何使用jquery显示具体的li数
- 一个接一个地淡出每一个li
- 使用li元素的html内容更改该元素的背景
- jQuery检查LI's content-基于内容添加特定类-jQuery出现问题's$(这个)