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

| 我有一段HTML代码,其中包含条件注释:
<!--[if IE]>
<link rel=\"stylesheet\" ...>
<[endif]-->
此代码经过测试,在初始页面呈现时包含在页面的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.
上面的代码不会将样式表应用于Internet Explorer7。是否可以使用JavaScript以Internet Explorer可以理解的方式引入条件样式? 为了清楚起见,我试图使用JavaScript创建条件样式,而不是尝试编写浏览器条件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);
}
    
很简单的:
/*@cc_on document.createStyleSheet(\"ie.css\"); @*/
    
你知道Modernizr吗? 另外,在HTML open标签中使用它非常好(用
js
代替
no-js
!)
<!--[if lt IE 7 ]><html lang=en-us class=\"no-js ie6\"><![endif]--> 
<!--[if IE 7 ]><html lang=en-us class=\"no-js ie7\"><![endif]--> 
<!--[if IE 8 ]><html lang=en-us class=\"no-js ie8\"><![endif]--> 
<!--[if (gte IE 9)|!(IE)]><!--> <html lang=en-us class=no-js> <!--<![endif]--> 
因此,您可以在CSS中执行此操作:
.ie7 .myDiv {
    foo:bar;
}
也可以使用javascript访问:
if( document.body.className.indexOf(\"ie\") > -1 ){
     //
}

// or using jQuery

if( $(document.body).hasClass(\"ie6\") ){
}
    
所有这些都是不必要的。 公共无效renderHead(IHeaderResponse响应) 如果(WebSession.get()。getClientInfo()。getClientProperties()。isBrowserInternetExplorer()){   response.renderJavaScriptReference(new JavaScriptResourceReference(MyClass.class,\“ ie.js \”)); } 如果您选择使用Modernizr,请注意https://issues.apache.org/jira/browse/WICKET-3433     

要回复问题请先登录注册