Get / Set innerHTML in Perl HTML :: TreeBuilder?

Get / Set innerHTML in Perl HTML :: TreeBuilder? I could get innerHTML but don't know how to install.

Thanks at Advance.

+4
source share
2 answers

I'm not sure if this approach will satisfy you, but you can use the html ($ html) method from pQuery :

This method is akin to the famous JavaScript / DOM function innerHTML.

If called without arguments, this will return the inner HTML string of the first DOM element in the pQuery object.

If invoked using an HTML string argument, this will set the internal HTML of all DOM elements to the pQuery object.

To what extent pQuery can satisfy you, to quote from POD:

pQuery :: DOM is roughly an attempt to duplicate JavaScript DOM in Perl. These are subclasses of HTML :: TreeBuilder / HTML :: Element there are several differences when realizing it. See the PQuery :: DOM documentation for details.

+1
source

I would use pQuery, but this will work

#!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder; my $html = <<'__HTML__'; <div id="target">old <B>i</B><I>n</I>ner</div> __HTML__ { my $t = HTML::TreeBuilder->new_from_content($html); print $t->as_HTML('<>&',' ',{}), "\n"; my $target = $t->look_down( id => 'target' ); $target->delete_content; $target->push_content( HTML::TreeBuilder->new_from_content( "<B>NEW</B>" )->look_down(qw!_tag body!)->detach_content ); print $t->as_HTML('<>&',' ',{}), "\n"; } __END__ <html> <head> </head> <body> <div id="target">old <b>i</b><i>n</i>ner</div> </body> </html> <html> <head> </head> <body> <div id="target"><b>NEW</b></div> </body> </html> 

Yes, I am RTFM

+1
source

Source: https://habr.com/ru/post/1311574/


All Articles