有没有办法使用JavaScript介绍Internet Explorer条件注释? [英] Is there a way to introduce Internet Explorer conditional comments using JavaScript?

查看:158
本文介绍了有没有办法使用JavaScript介绍Internet Explorer条件注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a segment of HTML code which includes a conditional comment:

<!--[if IE]>
<link rel="stylesheet" ...>
<[endif]-->

此代码在包含在页面的HEAD部分,初始页

This code was tested and works correctly when included in the HEAD section of the page, on the initial page rendering.

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

I would like to introduce the same conditional CSS to an existing page using JavaScript in an Ajax response.

我试过:

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.

上述代码不会导致在Internet Explorer 7中应用样式表。使用JavaScript,以Internet Explorer能够理解的方式引入条件样式?

The above code does not cause the stylesheet to be applied in Internet Explorer 7. Is there a way, using JavaScript, to introduce the conditional style in a way that Internet Explorer understands?

为了清楚起见,我试图使用JavaScript来创建条件样式,

Just to be clear, I am trying to use JavaScript to create a conditional style, not trying to write browser-conditional JavaScript.

推荐答案

您可以在 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);
}

这篇关于有没有办法使用JavaScript介绍Internet Explorer条件注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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