微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-使用DomDocument添加条件注释

我见过一个与更改条件注释有关的线程,但是我找不到是否可以使用PHP dom功能添加新的条件注释.

我本质上希望能够将以下内容添加到其中(不是唯一的示例,但是您明白了!).

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->

我看过DomComment,但似乎在评论添加了结束标记,因此最终得到:

<!--[if ! lte IE 6]--><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->

解决方法:

这个:

<?PHP
$doc = new DOMDocument();
$doc->loadHTML("<html><body><p id='bla'>Test</body></html>");
$bla = $doc->getElementById("bla");
$bla->appendChild(new DOMComment('[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]'));

echo $doc->saveHTML();  //<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]-->

为我工作.请注意,条件注释的proper syntax

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]-->

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->

如您所说,您想要拥有它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐