gpt4 book ai didi

javascript - jQuery 将 li 附加到 ul

转载 作者:行者123 更新时间:2023-11-27 23:34:40 26 4
gpt4 key购买 nike

我想使用 jQuery 将图像添加到列表中

index.html:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="main.js"></script>
</head>
<body>
<table class="center">
<tr>
<td>
<ul id="fistImagesList"></ul>
</td>

<td>
<ul id="secondImagesList"></ul>
</td>

<td>
<ul id="thirdImagesList"></ul>
</td>

<td>
<ul id="fourthImagesList"></ul>
</td>
</tr>
</table>
</body>
</html>

和 jQuery:

(function ($) {
firstName = "";
secondName = "";

fillList("fistImagesList");
fillList("secondImagesList");
fillList("thirdImagesList");
fillList("fourthImagesList");

function fillList(name) {
for (var i = 0; i < 4; i++) {
var randomNumber = Math.floor(Math.random() * imgArray.length);


$("#" + name).append(
'<li><img src="' + imgArray[randomNumber].src + '" name="' + imgArray[randomNumber].name + '"/><li>'
);

}
}

var imgArray = new Array();

imgArray[0] = new Image();
imgArray[0].src = "images/earth.jpg";
imgArray[0].name = "earth";

imgArray[1] = new Image();
imgArray[1].src = "images/mars.jpg";
imgArray[1].name = "mars";


imgArray[2] = new Image();
imgArray[2].src = "images/saturn.jpg";
imgArray[2].name = "saturn";


imgArray[3] = new Image();
imgArray[3].src = "images/jupiter.png";
imgArray[3].name = "jupiter";
})(jQuery);

我用谷歌搜索了一下,在 stackoverflow.com 和其他网站上看到了很多关于 append() 和 appendTo() 的帖子,但是当浏览器编译我的文件时没有任何反应。

也许我使用了错误的 jquery 选择器? $("#"+姓名)。但是当我使用 $("table.center>tbody>tr>td>ul#"+name)$("table.center>tr>td>ul#"+name ) 什么也没有发生。

最佳答案

移动这个:

fillList("fistImagesList");
fillList("secondImagesList");
fillList("thirdImagesList");
fillList("fourthImagesList");

到代码的底部。当您在顶部使用它时,您的 imgArray 仍然是空的。

关于javascript - jQuery 将 li 附加到 ul,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34384687/

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