简体   繁体   English

单击按钮后如何更改图像

[英]How do I get the image to change when the button is clicked

Im sorry to be such a noob but when it comes to programming i have a lot of difficulty so if someone could help that would be great! 很抱歉成为这样的菜鸟,但是在编程方面,我遇到了很多困难,因此,如果有人可以帮助的话,那就太好了!

Basically I am trying to make a zindex with a few images in which I'd like when I click the button it switches the image. 基本上,我试图用一些图像制作一个zindex,当我单击按钮时会在其中切换图像。 I know this is probably very basic but I am very lost on what to do. 我知道这可能是非常基本的,但是我对要做的事情很迷失。 I understand the concept but cannot input it. 我了解这个概念,但无法输入。

I am also trying to get the images to be in the middle of the page. 我也试图将图像放在页面中间。 I was recommended to the following; 我被推荐给以下人士; however, that did not work. 但是,那没有用。 Can someone please help? 有人可以帮忙吗?

Here is what I have: 这是我所拥有的:

CSS 的CSS

#xbone {
    position:absolute;
    left:0px;
    top:0px;
    margin-left: auto;
    margin-right: auto;
    z-index:-1
}
#ps4 {
    position:absolute;
    left:0px;
    top:0px;
    margin-left: auto;
    margin-right: auto;
    z-index:-2
}
#wiiu {
    position:absolute;
    left:0px;
    top:0px;
    margin-left: auto;
    margin-right: auto;
    z-index:-3
}

JavaScript 的JavaScript

function changeStackOrder() {
    document.getElementById("xbone").style.zIndex="1";
}

HTML 的HTML

<img id="xbone" alt="Xbox One" width="800" height="600"
     src="http://assets1.ignimgs.com/vid/thumbnails/user/2013/06/19/XboxOne1.jpg">
<img id="ps4" alt="Playstation 4" width="800" height="600"
     src="http://www.nowgamer.com/siteimage/scale/0/0/360062.jpg" >
<img id="wiiu" alt="Wii U" width="800" height="600"
     src="http://venturebeat.files.wordpress.com/2013/07/wii-u.jpg" >
<input type="button" onclick="changeStackOrder()" value="Change stack order">

JsFiddle: http://jsfiddle.net/GkGJw/ JsFiddle: http : //jsfiddle.net/GkGJw/

It is not changing because setting the zIndex of xbone to one still makes it on top. 它没有改变,因为将xbone的zIndex设置为1仍然使它位于顶部。 If you would do it for any of the other images it would appear on top. 如果您要对其他任何图像执行此操作,它将显示在顶部。

function changeStackOrder() {
    $("img").each(function() {
        $(this).css("zIndex", "+=1");
        if($(this).css("zIndex") == "0"){
            $(this).css("zIndex", "-3");
        }
    });
}

Try something like this 试试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM