DWQA QuestionsCategory: ProgramHow to cancel the label package gracefully?
linkgod asked 9 months ago

There’s a piece of HTML like this

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

After I delete it like this


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();

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 9 months 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 9 months ago
var el = document.querySelector('.markpen-mark');
el.previousSibling.textContent += el.textContent + el.nextSibling.textContent;

Make your own judgment in special circumstances.

monkey answered 9 months ago

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


var p=document.querySelector('p');
    if(node.nodeType != 3)
        p.replaceChild(document.createTextNode(node.textContent), node)

In this way, the elements inside P become threeTextNodeType.

debbbbie answered 9 months 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! → → → → →