jQuery: Umgebendes HTML Element (Wrapper) entfernen und Inhalte beibehalten

jQuery JavaScript Framework

Heute ein Mini-Script für die JQuery Freunde.

Ziel war es, ein Element von seinem umgebenden HTML-Element freizustellen, dabei jedoch die Inhalte beizubehalten.

Lösung war für mich das folgende Script:

  1.     /**
  2.     * Entfernt das äußerste Wrapper-Element innerhalb des Selektors.
  3.     * Beispiel: <a href="../target.html"><strong>Content</strong></a> ==>  <a href="../target.html">Content</a>
  4.     */
  5.    function removeChildWrapper(selector){
  6.            $(selector).each(function(){
  7.                    $(this).html($(this).children().html());
  8.            });
  9.    }

Vielleicht hilft es ja dem ein oder anderen auch bei derselben Problemstellung?
Oder habt ihr eine bessere Lösung, ggf. sogar eine passende jQuery Methode?

Ich freue mich über Anregungen und Verbesserungen.

Einordnung:

Comments

Oliver's picture

http://api.jquery.com/unwrap/

Julian Pustkuchen's picture

Hey, vielen Dank für den

Hey, vielen Dank für den Hinweis, das ist ja schon fast (vielleicht!) peinlich, dass ich das nicht gefunden habe.

An dieser Stelle ging es allerdings direkt um das Elternelement eines Text-Node. Ich werde morgen direkt mal testen, ob das dort auch funktioniert.

Beispiel:

  1. <p><strong>Text</strong></p>

<-- Strong entfernen! Wie lautet der Selektor mit unwrap?

Vielleicht:

  1. $('strong').children().unwrap();

?

Hat hier jemand Erfahrung?

Julian Pustkuchen's picture

Also spontan bekomme ich für

Also spontan bekomme ich für Textnodes keine funktionierende Lösung hin. Habt ihr eine?
(Ist dann erstmal doch nicht so peinlich, hoffe ich)

Eine kurze Alternative zu meiner Lösung ist aber vielleicht?

  1. $('.my-wrapper').replaceWith( $(this).contents() );

Oliver's picture

$('strong').replaceWith(this.

$('strong').replaceWith(this.childNodes);

Das müsste den Zweck erfüllen Smile

Julian Pustkuchen's picture

In meinem Test werden dadurch

In meinem Test werden dadurch gerade die Texte (Textnode Inhalte) auch entfernt. Klappt also nicht. Kannst du das Problem bestätigen?

Oliver's picture

Laut jQuery Api sollte sowas

Laut jQuery Api sollte sowas auch funktionieren. Aber ich hab jetzt weder das noch das letzte selbst probiert. Smile

$('strong').contents().unwrap();

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.
  • Pairs of<blockquote> tags will be styled as a block that indicates a quotation.
  • You can enable syntax highlighting of source code with the following tags: <c>, <cpp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <smarty>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Textual smileys will be replaced with graphical ones.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.