简体   繁体   English

如何删除外部标签( <tag></tag> ),然后使用jquery将其余内容附加到其父级?

[英]How to remove the outer tags(<tag></tag>) and append the rest to its parent using jquery?

Here iam developing a chrome extenstion which selects one element and then we need to remove the outer tag and append the rest to same previous tag. 在这里,我要开发一种铬扩展,选择一个元素,然后我们需要除去外部标签,并将其余标签附加到相同的先前标签。

Suppose my html structure is like this.. 假设我的html结构是这样的。

<span>
<marquee behavior="scroll" direction="up" scrolldelay="75" scrollamount="1" width="450" onmouseover="this.setAttribute('scrollamount', 0, 0);" onmouseout="this.setAttribute('scrollamount', 1, 0);" style="width: 450px;">

<table cellspacing="0" cellpadding="3" border="0" style="border-collapse: collapse" bordercolor="#000000" width="450">

<tbody>
<tr>
<td colspan="3"><b><u>Message</u></b>&nbsp;<img src="images/sir.jpg"></td>
</tr>


</tbody>
</table>
</marquee>
</span>

All i have is selected span element and then selected marquee element.So i want to remove the marquee tag and append it back to span tag. 我所拥有的全部是选择span元素,然后选择marquee element.So,我想删除marquee标签并将其附加回span标签。

Final structure should look like this... 最终结构应如下所示:

<span>

<table cellspacing="0" cellpadding="3" border="0" style="border-collapse: collapse" bordercolor="#000000" width="450">

<tbody>
<tr>
<td colspan="3"><b><u>Message</u></b>&nbsp;<img src="images/sir.jpg"></td>
</tr>


</tbody>
</table>
</span>

Code i have written : 我写的代码:

$(document).ready(function () {
    console.log("ready!");

    function getinfo() {
        message = document.getElementsByTagName('span');
        message_length =message.length -1;
        message_body = message[message_length];
        console.log(message_body);

        var child = message_body.children[0];

        console.log(child);
        //Upto here i hace selected the marquee element
        //Now remove the <marquee> tag
        //Also remove the </marquee> tag
        //Append it back to the span tag

    }

    setTimeout(getinfo, 10000);

});

可以使用unwrap()

$('marquee table').unwrap()

You can use contents to get the content of the marquee tag and then replace marquess with child dom 您可以使用内容获取marquee标记的内容,然后用子dom替换marquee

Since you have tagged this question 由于您已标记此问题

 function getinfo() {
       var childDom = $("marquee").contents();
      $("marquee").replaceWith(childDom);

    }

JSFIDDLE JSFIDDLE

You can also use unwrap to remove the parent element 您还可以使用unwrap移除父元素

function getinfo() {
    var getChild = $('table');
       getChild.unwrap()
   }

JSFIDDLE with unwrap method 具有解包方法的JSFIDDLE

Note: Since you have tagged it with jquery so usng jquery, though I dont see jquery in your code 注意:由于您已经用jquery标记了它,所以使用了jquery,尽管我在您的代码中看不到jquery

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

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