gpt4 book ai didi

javascript - 在 Javascript 和 HTML 中显示图像

转载 作者:可可西里 更新时间:2023-11-01 13:28:42 26 4
gpt4 key购买 nike

我目前正在做一项学校练习,但我找不到如何做一件事。

所以我有这个:

enter image description here

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

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

这是我的代码:

<!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>

最佳答案

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

关于javascript - 在 Javascript 和 HTML 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33598405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com