在 Perl HTML::TreeBuilder 中获取/设置innerHTML?

发布于 2024-09-04 00:53:30 字数 86 浏览 2 评论 0原文

在 Perl HTML::TreeBuilder 中获取/设置innerHTML?我可以获得innerHTML,但不知道如何设置。

提前致谢。

Get/Set innerHTML in Perl HTML::TreeBuilder? I could get innerHTML but dont know how to set.

Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角迷惘 2024-09-11 00:53:30

我不确定这种方法是否会让您满意,但您可以使用 html($html) 方法来自 pQuery:

这个方法类似于著名的
JavaScript/DOM 函数innerHTML。

如果不带参数调用,这将
返回内部 HTML 字符串
pQuery 中的第一个 DOM 元素
对象。

如果使用 HTML 字符串调用
参数,这将设置内部 HTML
pQuery 中所有 DOM 元素的数量
对象。

至于为什么 pQuery 可能会让你满意,引用 POD 的话:

pQuery::DOM 大致是一种尝试
在 Perl 中复制 JavaScript 的 DOM。它
子类
HTML::TreeBuilder/HTML::Element 所以
有一些差异
意识到。请参阅 pQuery::DOM
有关详细信息的文档。

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

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

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

If called with an HTML string
argument, this will set the inner HTML
of all the DOM elements in the pQuery
object.

As far as why pQuery may satisfy you, to quote from POD:

pQuery::DOM is roughly an attempt to
duplicate JavaScript's DOM in Perl. It
subclasses
HTML::TreeBuilder/HTML::Element so
there are a few differences to be
aware of. See the pQuery::DOM
documentation for details.

空城旧梦 2024-09-11 00:53:30

我会使用 pQuery,但这会起作用

#!/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>

是的,我 RTFM

I'd 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 RTFM

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文