简体   繁体   English

使用DomDocument添加条件注释

[英]Adding Conditional Comments using DomDocument

I've seen a thread relating to altering conditional comments, but I can't find whether it's possible to add new conditional comments using the php dom functionality. 我见过一个与更改条件注释相关的线程,但我无法找到是否可以使用php dom功能添加新的条件注释。

I essentially want to be able to add the following into (not the only example, but you get the idea!). 我本质上希望能够添加以下内容(不是唯一的例子,但你明白了!)。

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

I've looked at DomComment but it seems to add the closing tag for the comment, so I end up with: 我看过DomComment,但它似乎为评论添加了结束标记,所以我最终得到:

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

this: 这个:

<?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]-->

works for me. 适合我。 note that the proper syntax for a conditional comment is 请注意,条件注释的正确语法

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

not

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

as you say you want to have it. 正如你所说,你想拥有它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM