最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery : Removing parent tag not the child of that parent - Stack Overflow

programmeradmin3浏览0评论

i am trying to remove parent tag, but removing parent tag should not remove their child element. The child should get attached previous tag

Below is the code:

<div class="mask">
<ul id="TickerT" class="news">
<ul class="tweet_list">
<li class="first odd">
<span class="tweet_time">about 8 minutes ago</a></span>
<span class="tweet_join"></span>
<span class="tweet_text">test test…</a></span>
</li>
</ul>
</ul>
</div>
</div>

There are at-least 5 <li> and now i want to delete <ul class="tweet_list">.
I have tried it with jQuery's replaceWith but no result.

Here's the code i have used from a site
var jj = $("ul.tweet_list").contents()
$(".tweet_list").replaceWith(jj);

But this didn't worked.

What i wanted is i want to remove <ul class="tweet_list"> but not <li> of that <ul>.


Help appreciated, Thanks..

i am trying to remove parent tag, but removing parent tag should not remove their child element. The child should get attached previous tag

Below is the code:

<div class="mask">
<ul id="TickerT" class="news">
<ul class="tweet_list">
<li class="first odd">
<span class="tweet_time">about 8 minutes ago</a></span>
<span class="tweet_join"></span>
<span class="tweet_text">test test…</a></span>
</li>
</ul>
</ul>
</div>
</div>

There are at-least 5 <li> and now i want to delete <ul class="tweet_list">.
I have tried it with jQuery's replaceWith but no result.

Here's the code i have used from a site
var jj = $("ul.tweet_list").contents()
$(".tweet_list").replaceWith(jj);

But this didn't worked.

What i wanted is i want to remove <ul class="tweet_list"> but not <li> of that <ul>.


Help appreciated, Thanks..

Share Improve this question asked Feb 9, 2012 at 10:45 harnishharnish 772 silver badges13 bronze badges 1
  • possible duplicate of stackoverflow./questions/5936887/… – goodeye Commented Jul 7, 2014 at 2:59
Add a ment  | 

4 Answers 4

Reset to default 5

You can call the unwrap() method on your list item:

$("ul.tweet_list > li").unwrap();

This will remove the .tweet_list <ul> element and reparent the <li> element (and its siblings, if any) under the #TickerT <ul> element.

Just use the .unwrap() method:

$('.tweet_list').children().unwrap();

DEMO

var jj = $("ul.tweet_list").html()
$("ul.tweet_list").replaceWith(jj)

There is no content method on jquery. Try the html method.

You could save the .html() content and the parent ul of the list you want to delete into a variable and then add the .html() to the parent ul.

发布评论

评论列表(0)

  1. 暂无评论