简单的HTML Dom解析器:如何插入元素 [英] Simple HTML Dom Parser: How to insert to elements

查看:102
本文介绍了简单的HTML Dom解析器:如何插入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将元素(具体地是"body")插入(附加).

I am trying to insert (append) into an element ... "body" specifically.

目前,我可以通过这种方式执行此操作:

I am able to do this this way at the moment:

$var = "some JS stuff";
$e = $htmlDOM->find("body", 0);
$e->outertext = '<body>' . $e->innertext . $var . '</body>';

我的问题是,它可以修复<body>,但是实际的html可能将js或id附加到<body>上,如果是这种情况,我希望保持这种状态.

My issue is that this fixes <body> but the actual html might have js or id etc attached to <body> and I will like to maintain that if it is the case.

文档似乎未显示出用于此目的的方法.

The docs don't seem to show a method for this.

有可能吗?

推荐答案

查看源代码后,我发现解决方法是使用

After looking at the source code, I found that the way to go about it is to use

$var = "some JS stuff";
$e = $htmlDOM->find("body", 0);
$e->outertext = $e->makeup() . $e->innertext . $var . '</body>';

也就是说,未记录的makeup()函数将构建标记和任何关联的文本/代码.

That is, the undocumented makeup() function will build the tag and any associated text/code.

这篇关于简单的HTML Dom解析器:如何插入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆