有没有办法使用JavaScript引入Internet Explorer条件注释?

RMo*_*sey 2 javascript css dom wicket conditional-comments

我有一段HTML代码,其中包含条件注释:

<!--[if IE]>
<link rel="stylesheet" ...>
<[endif]-->
Run Code Online (Sandbox Code Playgroud)

当包含在页面的HEAD部分中,初始页面呈现时,此代码已经过测试并且可以正常工作.

我想在Ajax响应中使用JavaScript向现有页面引入相同的条件CSS.

我试过了:

var comment = document.createComment("[if IE]>\n<link rel=\"stylesheet\" ...>\n<[endif]");
Wicket.Head.addElement(comment); //this is framework code that just appends the element to the HEAD node of the page. I debugged and verified that it's doing the appendChild(...) call.
Run Code Online (Sandbox Code Playgroud)

上面的代码不会导致样式表在Internet Explorer 7中应用.有没有办法使用JavaScript以Internet Explorer理解的方式引入条件样式?

为了清楚起见,我正在尝试使用JavaScript来创建条件样式,而不是尝试编写具有浏览器条件的JavaScript.

Tim*_*own 5

您可以在里面使用条件注释innerHTML,如下所示:

var div = document.createElement("div");
div.innerHTML = "<!--[if IE]><i></i><![endif]-->";
if (div.getElementsByTagName("i").length) {
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = "ie_only.css";
    document.getElementsByTagName("head")[0].appendChild(link);
}
Run Code Online (Sandbox Code Playgroud)