DWQA QuestionsCategory: ProgramHow to cancel the label package gracefully?
linkgod asked 1 week ago

There’s a piece of HTML like this

<p>
    Until a week ago,
    < span class = "markpen mark" > I said to convective sand</span>
    Let the wind blow me away.
</p>

After I delete it like this

$('.markpen-mark').contents().unwrap();

It’s going to be like this
How to cancel the label package gracefully?
How to delete it and make it like this? The childNodes in the tag P becomes three “text”
How to cancel the label package gracefully?
Online example http://codepen.io/linkmod/pen/hrmaj,
I tried it, too

var parent = $('.markpen-mark').parent();
$('.markpen-mark').contents().unwrap();
parent.html(parent.html());

However, the disadvantage of this is that when there are events bound to other tags in the parent, the event cannot be triggered.
Is there any elegant solution?

Childe replied 1 week ago

var mark=$('.markpen-mark').parent(); mark.html(mark.text().replace(/[\r\n| ]/g, '')Can achieve the effect, but not elegant enough, ah!

3 Answers
Evian answered 1 week ago
var el = document.querySelector('.markpen-mark');
el.previousSibling.textContent += el.textContent + el.nextSibling.textContent;
el.parentNode.removeChild(el.nextSibling);
el.parentNode.removeChild(el);

Make your own judgment in special circumstances.

monkey answered 1 week ago

Is to let<p>Do all elements inside the tag become nodes of type textnode?
You can refer to the following:

document.body.innerHTML='<p>aaa<span>bb</span>ccc</p>';

var p=document.querySelector('p');
[].slice.call(p.childNodes).forEach(function(node){
    if(node.nodeType != 3)
        p.replaceChild(document.createTextNode(node.textContent), node)
});

In this way, the elements inside P become threeTextNodeType.

debbbbie answered 1 week ago

I won’t tell you, right now$("#aaa").html()Already been"Until a week ago, I told quicksand to let the wind blow me away. "Did you?
请输入图片描述

aboutjsIt’s very elegant. The only thing that’s not elegant is the debugging interface! → → → → →
请输入代码