【问题标题】:display image in Javascript and HTML在 Javascript 和 HTML 中显示图像
【发布时间】:2016-02-09 11:11:13
【问题描述】:

我目前正在做一个学校练习,但我不知道怎么做。

所以我有这个:

当我单击其中一个案例时,应该显示正确的图像(例如图像是马里奥图片),但问题是我只得到它的名称。我想知道我必须更改什么才能显示图片而不是名称。

请让我知道我应该在我的代码中更改什么,我是初学者。非常感谢!

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>

    <style type="text/css">
/*<![CDATA[*/
    div#memory_board{
        background:#B0C4DE;
        border:#999 1px solid;
        width:580px;
        height:580px;
        padding:0px;
        margin:45px auto;
    }
    div#memory_board > div{
        background: url(images/memoryBg.png) no-repeat;
        border:none;
        width:71px;
        height:71px;
        float:left;
        margin:35px;
        padding:0px;
        font-size:30px;
        cursor:pointer;
        text-align:center;
    }
    /*]]>*/
    </style>
    <script type="text/javascript">
//<![CDATA[
    var user = prompt ("Benutzername eingeben und zum Starten auf OK klicken!");
    console.log("Willkommen " + user + "!");
    document.write ("Willkommen " + user + "!");    

    var memory_array = [document.getElementById('url(images/card1.png) no-repeat'),document.getElementById('url(images/card1.png) no-repeat'),document.getElementById('card2'),document.getElementById('card2'),document.getElementById('card3')document.getElementById('card3')document.getElementById('card4')document.getElementById('card4')document.getElementById('card5')document.getElementById('card5')document.getElementById('card6')document.getElementById('card6')document.getElementById('card7')document.getElementById('card7')document.getElementById('card8')document.getElementById('card8')];
    var memory_values = [];
    var memory_tile_ids = [];
    var tiles_flipped = 0;
    Array.prototype.memory_tile_shuffle = function(){
    var i = this.length, j, temp;
    while(--i > 0){
        j = Math.floor(Math.random() * (i+1));
        temp = this[j];
        this[j] = this[i];
        this[i] = temp;
    }
    }
    function newBoard(){
        tiles_flipped = 0;
        var output = '';
    memory_array.memory_tile_shuffle();
        for(var i = 0; i < memory_array.length; i++){
                output += '<div id="tile_'+i+'" onclick="memoryFlipTile(this,\''+memory_array[i]+'\')"><\/div>';
        }
        document.getElementById('memory_board').innerHTML = output;
    }
    function memoryFlipTile(tile,val){
        if(tile.innerHTML == "" && memory_values.length < 2){
                tile.style.background = '#FFF';
                tile.innerHTML = val;
                if(memory_values.length == 0){
                        memory_values.push(val);
                        memory_tile_ids.push(tile.id);
                } else if(memory_values.length == 1){
                        memory_values.push(val);
                        memory_tile_ids.push(tile.id);
                        if(memory_values[0] == memory_values[1]){
                                tiles_flipped += 2;
                                // Clear both arrays
                                memory_values = [];
                memory_tile_ids = [];
                                // Check to see if the whole board is cleared
                                if(tiles_flipped == memory_array.length){
                                        alert("Toll gemacht! Neues Spielfeld wird generiert. Um fortzufahren, klicken Sie auf OK");
                                        document.getElementById('memory_board').innerHTML = "";
                                        newBoard();
                                }
                        } else {
                                function flip2Back(){
                                    // Flip the 2 tiles back over
                                    var tile_1 = document.getElementById(memory_tile_ids[0]);
                                    var tile_2 = document.getElementById(memory_tile_ids[1]);
                                    tile_1.style.background = 'url(images/memoryBg.png) no-repeat';
                    tile_1.innerHTML = "";
                                    tile_2.style.background = 'url(images/memoryBg.png) no-repeat';
                    tile_2.innerHTML = "";
                                    // Clear both arrays
                                    memory_values = [];
                    memory_tile_ids = [];
                                }
                                setTimeout(flip2Back, 700);
                        }
                }
        }
    }
    //]]>
    </script>
    <title></title>
  </head>
  <body>
    <div id="memory_board"></div><script type="text/javascript">
//<![CDATA[
    newBoard();
    //]]>
    </script>
  </body>
</html>

【问题讨论】:

    标签: javascript html css image random


    【解决方案1】:

    我认为你需要将你的css中的url放在引号内。如果这样做没有帮助,请使用完整地址而不是相对地址。

    【讨论】:

    • 谢谢你,但它没有用,或者我没有用正确的方式做...你能告诉我你的意思吗?我的图像在文件夹 image 中
    • url() 里面需要字符串,比如 background: url("images/memoryBg.png") no-repeat; ....我可以' t 让代码块工作。但无论如何,我的意思是您需要将图像地址放在 " 引号内。
    • 好吧,我按照你的要求做了,现在发生了一些奇怪的事情,当我点击它们时,案件正在消失......这很奇怪。
    • 您是否记得对代码中使用 url() 的所有位置进行“更改”?听起来它几乎正在做您计划要做的事情,但是可能是缺少 " 或逻辑中的其他东西导致了这种情况。
    • 是的,我在任何地方都进行了更改,但它不起作用。您能否准确地告诉我您将如何在代码中编写它,例如名为“card1.png”的图像?谢谢!
    【解决方案2】:

    使用背景:url("images/memoryBg.png") no-repeat;

    代替背景:url(images/memoryBg.png) no-repeat;

    【讨论】:

    • 它没有改变任何东西,这些图像只是背景,我想要展示一些图像,你可以看到我的照片上写着“image1”.. 我希望我足够清楚...
    猜你喜欢
    • 2011-05-05
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2013-12-08
    相关资源
    最近更新 更多