简体   繁体   English

jQuery-从“ this”中删除特定的元素标签(而不是HTML)?

[英]JQuery - Remove specific element tags (but not the HTML) from within 'this'?

I have a table (with 2 columns) I want to convert into a list. 我有一张表(有2列),我想转换成列表。

So far I can wrap the tr s in li tags eg: 到目前为止,我可以将tr s包装在li标签中,例如:

$('#table tr').each(function() {
  $(this).wrap('<li />');
});

But I was wondering if there was a way to simply then strip the <tr> and <td> tags out from withing this just leaving whatever other written content there is? 但我不知道是否有一种方式来简单地再剥离<tr><td>从withing标记出来this只是留下任何其它书面内容有?

I have tried messing around with remove() and replaceWith() but they remove everything from between the tags as well. 我曾尝试使用remove()replaceWith()但它们也会同时删除标记之间的所有内容。

Is there a way to just target the tags themselves? 有没有办法只针对标签本身?

EDIT: HTML: 编辑:HTML:

This: 这个:

<tr>
 <td>Lorem</td>
 <td>Ipsum</td>
</tr>
<tr>
 <td>Dolar</td>
 <td>Sit</td>
</tr>

Becomes: 变为:

<li>Lorem Ipsum</li>
<li>Dolar Sit</li>

*obviously will deal with the spaces between the words later *显然以后会处理单词之间的空格

Try this, 尝试这个,

$(function(){
    $('tr').each(function(){
        console.log('<li>'+$(this).text()+'</li>')
        $(this).html('<li>'+$(this).text()+'</li>');
    });
});

Fiddle http://jsfiddle.net/wwSGV/1 小提琴 http://jsfiddle.net/wwSGV/1

http://jsfiddle.net/m3emb/1/ http://jsfiddle.net/m3emb/1/

html HTML

<table>
<tr>
 <td>Lorem</td>
 <td>Ipsum</td>
</tr>
<tr>
 <td>Dolar</td>
 <td>Sit</td>
</tr>
</table>

<ul></ul>

jQuery jQuery的

var $list = $('ul');
$('table tr').each(function() {
  $list.append($('<li />').html($(this).text()));
});
$('table').remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM