如何从if语句中显示图像

时间:2016-11-10 01:17:31

标签: javascript html image

<img id="myImg">
<script>
    var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52];

    function myFunction() {
        deck.sort(function(a, b) {
            return 0.5 - Math.random()
        });
        deck.shift();
        document.getElementById("demo").innerHTML = deck;
        document.getElementById("player1").innerHTML = deck[0];

        if (deck[0] == '1') {
            document.getElementById("myImg").style.display = 'inline';
        } else if (deck[0] == '2') {
            alert("I am an alert box!");

这是我得到的代码。我需要它来在特定位置显示图像。

1 个答案:

答案 0 :(得分:0)

&lt; p&gt;一些错误: &LT;李&GT;缺少&lt; code&gt;}&lt; / code&gt;&lt; / p&gt; &lt; p&gt;请参阅代码段&lt; / p&gt; &lt; p&gt;注意:我添加了一个图像并将其display属性设置为none。 &LT; / p为H. &lt; p&gt;如果该代码段运行了几次,并且您很幸运能够获得包含第一个和第二个元素的数组,并且&lt; code&gt; 1&lt; / code&gt;和&lt; code&gt; 2&lt; / code&gt;您将注意到if语句已执行&lt; / p&gt; &lt; p&gt;&lt; div class =&#34; snippet&#34;数据琅=&#34; JS&#34;数据隐藏=&#34;假&#34;数据的控制台=&#34;真&#34;数据巴贝尔=&#34;假&#34;&GT;&#13; &lt; div class =&#34; snippet-code&#34;&gt;&#13; &lt; pre class =&#34; snippet-code-css lang-css prettyprint-override&#34;&gt;&lt; code&gt; img {&#13;   display:none;&#13; }&LT; /代码&GT;&LT; /预&GT;&#13; &lt; pre class =&#34; snippet-code-html lang-html prettyprint-override&#34;&gt;&lt; code&gt;&lt; img id =&#34; myImg&#34; SRC =&#34; HTTPS://encrypted-tbn2.gstatic.com/images Q = TBN:ANd9GcTkb6sdx_0T-5Pb34CB1_x1ILIQIErjC5C1kA8cIrxDclawMiMK&#34;&GT;&#13; &lt; div id =&#34; demo&#34;&gt;&lt; / div&gt;&#13; &lt; div id =&#34; player1&#34;&gt;&lt; / div&gt;&#13; &LT;脚本&GT;&#13;   var deck = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, 24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48, 49,50,51,52];&#13; &#13;   function myFunction(){&#13;     deck.sort(function(a,b){&#13;       返回0.5 - Math.random()&#13;     });&#13;     deck.shift();&#13;     document.getElementById(&#34; demo&#34;)。innerHTML = deck;&#13;     document.getElementById(&#34; player1&#34;)。innerHTML = deck [0];&#13;     if(deck [0] == 1){&#13;       document.getElementById(&#34; myImg&#34;)。style.display =&#39; inline&#39;;&#13;     } else if(deck [0] == 2){&#13;       警报(&#34;我是一个警报框!&#34;);&#13;     }&#13;   }&#13;   myFunction的()&#13; &LT; /脚本&GT;&LT; /代码&GT;&LT; /预&GT;&#13; &LT; / DIV&GT;&#13; &LT; / DIV&GT;&#13; &LT; / p为H.