La fonction unwrap() en JS pur ?
Salut,
Cette fonction est notamment utile pour supprimer la surbrillance des mots trouvés, dans ce code : http://jsbin.com/lapikifose/edit?js,output j'ai testé trois fonctions "removeHightlight"...
Le plus souvent pour la fonction unwrap() en JS pure je vois ça :
Code:
1 2 3 4 5 6 7
| function unwrap0(elm) {
var parent = elm.parentNode;
while (elm.firstChild) {
parent.insertBefore(elm.firstChild, elm);
}
parent.removeChild(elm);
} |
Mais que pensez-vous de celle-ci :
Code:
1 2 3
| function unwrap1(elm) {
elm.outerHTML = elm.innerHTML;
} |
Je crois que ce n'est pas bien vu mais pourtant elle est plus courte et je crois plus rapide, non ?
De plus pour la fonction removeHightlight1() il faut rajouter la fonction normalize() pour retrouver le DOM comme il était avant la surbrillance (joindre les nodetexts qui ont été divisés) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| function removeHightlight1() {
var hightlightSpanTab = document.getElementsByClassName("hightlightClass");
while (hightlightSpanTab.length) {
var parent = hightlightSpanTab[0].parentNode;
while (hightlightSpanTab[0].firstChild) {
parent.insertBefore(hightlightSpanTab[0].firstChild, hightlightSpanTab[0]);
}
parent.removeChild(hightlightSpanTab[0]);
parent.normalize();
}
} |
Mais j'ai remarqué qu'on en avait pas besoin quand on utilise la fonction unwrap1(elm) :
Code:
1 2 3 4 5 6 7 8
| function removeHightlight2() {
var hightlightSpanTab = document.getElementsByClassName("hightlightClass");
while (hightlightSpanTab.length) {
hightlightSpanTab[0].outerHTML = hightlightSpanTab[0].innerHTML;
}
} |
Ce qui est encore un avantage, non ?
Alors trouvez-vous des inconvénients à cette fonction ?
Merci.