簡體   English   中英

如何在innerHTML中嵌入IE的條件注釋?

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

好的,所以我想有條件地添加這行代碼;

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

使用方法:

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

if()/else()語句中,它不寫! 如果我擺脫了選擇性注釋( <!--[if ! IE]><![endif]--> )並僅放置SVG( <embed src="logo.svg" type="image/svg+xml" /> )工作! 我該怎么辦?

我找到了解決方法,但我認為在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>

最好不要將其放在innerHTML中,而是使用條件來控制它是否進入innerHTML中,如下所示:

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

如果您向我們展示了您要解決的整個問題,則可能有一些方法可以在沒有瀏覽器條件的情況下進行。

您檢測不到IE的語法已損壞。

Microsoft推薦的方式-導致標記錯誤

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

這種方式也顯示作品並且是有效的標記

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

也就是說,@ jfriend00的評論很好。

您的方式開始了一個永遠不會關閉的html comment ,因此無法正確處理。 html comments以第一個打開--並以第二個關閉。 它們不像常規元素一樣用>結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM