2011-02-14 44 views
3

我看到了一個與更改條件註釋有關的線程,但我無法找到是否可以使用php dom功能添加新的條件註釋。使用DomDocument添加條件註釋

我基本上希望能夠添加以下內容(不是唯一的例子,但你明白了!)。

<!--[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]--> 

回答

3

這樣的:

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

,你說你想擁有它。

+0

完美 - 不太清楚我怎麼沒有嘗試:)非常感謝! – Simon 2011-02-15 02:08:01