简体   繁体   English

如何在innerHTML中嵌入IE的条件注释?

[英]How can I embed a conditional comment for IE with innerHTML?

Ok so I want to conditionally add this line of code; 好的,所以我想有条件地添加这行代码;

<!--[if ! IE]> <embed src="logo.svg" type="image/svg+xml" /> <![endif]-->

Using: 使用方法:

document.getElementById("logo") .innerHTML='...';

In a if()/else() statement and it don't write it! if()/else()语句中,它不写! If i get rid of the selective comment ( <!--[if ! IE]><![endif]--> ) and only put the SVG ( <embed src="logo.svg" type="image/svg+xml" /> ) it work! 如果我摆脱了选择性注释( <!--[if ! IE]><![endif]--> )并仅放置SVG( <embed src="logo.svg" type="image/svg+xml" /> )工作! what should I do? 我该怎么办?

I found a way around but i think in the Android browser the thing will pop up twice. 我找到了解决方法,但我认为在Android浏览器中,该事件将弹出两次。

here's what I've done ( and its Validated stuff!);

<!DOCTYPE html>
<html>
<head>
<META CHARSET="UTF-8">
<title>SVG Test</title>
<script type="text/javascript">
//<![CDATA[
onload=function()
    {

        var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {

        document.getElementById("logo").innerHTML='<img src="fin_palais.png"/>';

        }


    }
//]]>

</script>
</head>
<body>

    <div id="logo">
        <!--[if lt IE 9]>
            <img src="fin_palais.png"/>
        <![endif]-->

        <!--[if gte IE 9]><!-->

            <embed src="fin_palais.svg" type="image/svg+xml" />

        <!--<![endif]-->

    </div>

</body>

It's probably better to not put it in the innerHTML, but use the condition to control whether it goes in the innerHTML or not like this: 最好不要将其放在innerHTML中,而是使用条件来控制它是否进入innerHTML中,如下所示:

var str = '...';    // whatever your other HTML is
<!--[if ! IE]>
str += '<embed src="logo.svg" type="image/svg+xml" />';
<![endif]-->

If you show us the whole problem you're trying to solve, there are probably ways to do this without browser conditionals. 如果您向我们展示了您要解决的整个问题,则可能有一些方法可以在没有浏览器条件的情况下进行。

Your syntax for detecting not IE is broken. 您检测不到IE的语法已损坏。

Microsoft recommended way - results in bad markup Microsoft推荐的方式-导致标记错误

<![if !IE]>
 <p>This is shown in downlevel browsers, but is invalid HTML!</p>
<![endif]>

This way shows works too and is valid markup 这种方式也显示作品并且是有效的标记

<!--[if !IE]>-->
  <p>This is shown in downlevel browsers.</p>
<!--<![endif]-->

That said, @jfriend00's comment is good. 也就是说,@ jfriend00的评论很好。

Your way begins an html comment which is never closed, and thus doesn't process correctly. 您的方式开始了一个永远不会关闭的html comment ,因此无法正确处理。 html comments are opened with the first -- and closed with the second. html comments以第一个打开--并以第二个关闭。 They don't close with the > like regular elements. 它们不像常规元素一样用>结束。

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

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