Javascript 如何在单击按钮时导入/更改图像?
在主页中有3个图像和2个按钮,分别命名为“下一个”和“上一个”。单击“下一步”按钮时,3个图像将更改为下3个图像。总共有100张图片。然后单击“下一步”以显示新的3个图像。第三次单击时,仅显示最后一张图像。 在“上一步”按钮上,单击“执行反向”。代码在下面,未完成Javascript 如何在单击按钮时导入/更改图像?,javascript,Javascript,在主页中有3个图像和2个按钮,分别命名为“下一个”和“上一个”。单击“下一步”按钮时,3个图像将更改为下3个图像。总共有100张图片。然后单击“下一步”以显示新的3个图像。第三次单击时,仅显示最后一张图像。 在“上一步”按钮上,单击“执行反向”。代码在下面,未完成 <html> <head> <title> Check! 1 </title> <style type="text/css">
<html>
<head>
<title>
Check! 1
</title>
<style type="text/css">
#myimage {
position:absolute;
left:800;
top:500;
height: 50px;
width: 50px;
}
#myimage1 {
position:absolute;
left:500;
top:500;
height: 50px;
width: 50px;
}
#imageDiv
{
position:static;
top: 50px;
left: 10px;
}
</style>
<script type="text/javascript">
var count=0;
function image(thisImg) {
var img = document.createElement("IMG");
img.src = "img/"+thisImg;
document.getElementById('imageDiv').appendChild(img);
}
function test()
{
//alert("just checking yaar");
if(count<4)
{
++count;
console.log("count",count);
if(count==1)
{
image('44585_Murree-Best-Hill-Station-wallpapers- pictures_640x320.jpg');
image('91517_background-painting-used-Main-Menu-screen_640x320.jpg');
image('gravityflue04-640x320.jpg');
}
else if(count==2)
{
image('44585_Murree-Best-Hill-Station-wallpapers-pictures_640x320.jpg');
image('91517_background-painting-used-Main-Menu-screen_640x320.jpg');
image('gravityflue04-640x320.jpg');
}
else if(count==3)
{
image('44585_Murree-Best-Hill-Station-wallpapers-pictures_640x320.jpg');
image('91517_background-painting-used-Main-Menu-screen_640x320.jpg');
image('gravityflue04-640x320.jpg');
}
else if(count==4)
{
image('44585_Murree-Best-Hill-Station-wallpapers-pictures_640x320.jpg');
image('91517_background-painting-used-Main-Menu-screen_640x320.jpg');
image('gravityflue04-640x320.jpg');
}
else
{
console.log("Invalid Count");
}
}
}
function test2()
{
if(count>0)
{
--count;
console.log("count",count);
}
else
{
console.log("Invalid Count");
}
}
</script>
</head>
<body>
<input type="image" id="myimage" value="next" name="next" src="next-button.jpg" onclick="test()">
<input type="image" id="myimage1" value="" name="next" src="111645-glowing-green-neon-icon-media-a-media31-back.png" onclick="test2()">
这可能是jQuery最容易解决的问题 以下是您可以使用的解决方案: 下面是教程:加载带有图像名称的数组。图像中的空标记数决定了一次显示多少图像。将displayImage更改为指向包含图像的文件夹
欢迎来到堆栈溢出。在这里提问时,请提供一些代码,说明您尝试了什么,遇到了什么问题,等等。或者,如果没有什么可提供的,请不要问。function imagethisImg{var img=document.createElementIMG;img.src=img/+thisImg;document.getElementById'imageDiv'。appendChildimg;}我想用javascript创建它。请帮帮我……代码很脆弱。如果您向images div中添加了任何不是img的内容,那么代码将中断-因此,如果您希望将图像包装到css中未提供的内容中,则需要增强代码以选择图像。有一些功能可以让你做到这一点…就我个人而言,我更喜欢按钮在两个方向上一次移动一个图像-这样div总是显示三个图像-消除了隐藏不需要的img标签的需要。我一定喜欢那些飞行向下的投票。。。嗨,伙计,我爱死它了。。它起作用了。。但我需要打印100张图片。或者更多。。谢谢你的帮助,请帮我做那件事,很高兴它能起作用。我不懂打印100张图片的意思。这与这个问题有关还是一个新问题?如果是这样,最好是提出一个新问题
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<button onclick="previous();">previous</button>
<div id="images">
<img />
<img />
<img />
</div>
<button onclick="next();">next</button>
<script>
var imageSources = Array(
"orderedList0.png", "orderedList1.png", "orderedList2.png",
"orderedList3.png", "orderedList4.png", "orderedList5.png",
"orderedList6.png", "orderedList7.png", "orderedList8.png",
"orderedList9.png");
var firstImage = 0;
var increment = document.getElementById("images").children.length;
displayImages();
function next() {
if (firstImage + increment < imageSources.length) {
firstImage += increment;
}
displayImages();
}
function previous() {
if (firstImage - increment >= 0) {
firstImage -= increment;
}
displayImages();
}
function displayImages() {
var imageDiv = document.getElementById("images");
for (var imageIndex = 0; imageIndex < imageDiv.children.length ; imageIndex++) {
displayImage(imageDiv.children[imageIndex], imageSources[firstImage + imageIndex])
}
}
function displayImage(image, src) {
if (src) {
image.src = "images/" + src;
image.style.display = "";
} else {
image.style.display = "none";
}
}
</script>
</body>
</html>