Peamasii has asked for the wisdom of the Perl Monks concerning the following question:
The HTML is like:#!/usr/bin/perl -w use strict; use HTML::TreeBuilder; use HTML::Element; my $root = HTML::TreeBuilder->new_from_file("index.section.html"); my @imgs = $root->find_by_tag_name('img'); foreach my $img (@imgs) { my $new_parent = HTML::Element->new('a', 'href' => $img->attr('src'), +'rel' => 'lightbox'); $img->attr('class',"thumbnail"); $new_parent->push_content($img); $img->replace_with($new_parent); } print $root->as_HTML(); $root->delete; # erase this tree because we're done with it
<html> <body> <h2> Ajax Requests</h2> <img src="Daily/_Ajax_Requests.svg" alt="Daily Ajax Requests"> <img src="Hourly/_Ajax_Requests.svg" alt="Hourly Ajax Requests"> <img src="Group_by_Min/_Ajax_Requests.svg" alt="Group_by_Min Ajax Req +uests"> </body> </html>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTML Element replace
by smls (Friar) on May 17, 2014 at 10:07 UTC | |
by Peamasii (Sexton) on May 17, 2014 at 11:58 UTC | |
|
Re: HTML Element replace (error: Can't replace an item with its parent!)(cut/detach/clone)
by Anonymous Monk on May 17, 2014 at 10:07 UTC | |
by Peamasii (Sexton) on May 17, 2014 at 11:59 UTC |