1

問題があります。スクリプトは機能しますが、「未定義」が返されます。誰でも理由を知っていますか?

<div id="balise"></div>
<a href="ty.html">Telecharger</a>
<img src="aaa.png" id="mypic" alt="aa" />
<script>
    var OSName='',
        eight='windowseight.png',
        vista ='windowsvista.png',
        xp ='windowsxp.png';
        if (navigator.userAgent.indexOf("Windows NT 6.2")!=-1) {OSName="Windows 8"+document.getElementById('mypic').setAttribute('src',eight)};
        if (navigator.userAgent.indexOf("Windows NT 6.1")!=-1) {OSName="Windows 7"+document.getElementById('mypic').setAttribute('src','windowseven.png')};
        if (navigator.userAgent.indexOf("Windows NT 6.0")!=-1) {OSName="Windows Visa"+document.getElementById('mypic').setAttribute('src',vista)};
        if (navigator.userAgent.indexOf("Windows NT 5.1")!=-1) {OSName="Windows xp"+document.getElementById('mypic').setAttribute('src',windowsxp)};
        document.getElementById("balise").innerHTML = OSName;
</script>

ここに画像の説明を入力

4

1 に答える 1

0

私のおすすめ

<!DOCTYPE html>
<html>
<head>
<title>OS names</title>
<script type="text/javascript">
  var OS="unknown",
    OSObj={
        "seven"  :{ "name":"Windows 7",    "img":"windowseven.png"},
        "eight"  :{ "name":"Windows 8",    "img":"windowseight.png"},
        "vista"  :{ "name":"Windows Vista","img":"windowsvista.png"},
        "xp"     :{ "name":"Windows XP",   "img":"windowsxp.png"},
        "unknown":{ "name":"Not detected OS",      "img":"unknown.png"}
      };
  if (navigator.userAgent.indexOf("Windows NT 6.2")!=-1) OS="eight";
  if (navigator.userAgent.indexOf("Windows NT 6.1")!=-1) OS="seven";
  if (navigator.userAgent.indexOf("Windows NT 6.0")!=-1) OS="vista";
  if (navigator.userAgent.indexOf("Windows NT 5.1")!=-1) OS="xp";
  window.onload=function() {
    var thisOS = OSObj[OS];
    document.getElementById("balise").innerHTML = thisOS.name+'<img src="'+thisOS.img+'" alt="'+thisOS.name+'"/>';
  }
</script>
</head>
<body>
<div id="balise"></div>
<a href="ty.html">Telecharger</a>
</body>
</html>
于 2013-08-24T09:48:17.710 に答える