最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change picture in HTML using multiple buttons - Stack Overflow

programmeradmin1浏览0评论

I am going to have 5 images and 5 buttons, currently only the single button. On button click I would like to show only the image that is associated with the button. for that I would like to write single function that gets the image path as parameter. I am very new to JavaScript, Bellow is a code I found and edited a bit:

<script>
    function pictureChange(path) {
        console.log(path);
        document.getElementById("theImage").src=path;
    }
</script>
<body>
    <img id="theImage" src=".png">
    <p><input type="button" id="theButton" value="click me!" onclick="pictureChange(".png")"></p>
</body>

I am going to have 5 images and 5 buttons, currently only the single button. On button click I would like to show only the image that is associated with the button. for that I would like to write single function that gets the image path as parameter. I am very new to JavaScript, Bellow is a code I found and edited a bit:

<script>
    function pictureChange(path) {
        console.log(path);
        document.getElementById("theImage").src=path;
    }
</script>
<body>
    <img id="theImage" src="http://31.media.tumblr./18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
    <p><input type="button" id="theButton" value="click me!" onclick="pictureChange("http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png")"></p>
</body>
Share Improve this question edited Aug 21, 2017 at 9:27 Matthew Fitch 2651 gold badge9 silver badges13 bronze badges asked Aug 21, 2017 at 8:57 Tamir EinyTamir Einy 1861 silver badge13 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Currently issue in you code is, when you passing string in function you need to the sequence of inverted ma's. for example while using "", you can use only '' single inverted ma's inside of it.

<script>
function pictureChange(path) {
console.log(path);
document.getElementById("theImage").src=path;
}
</script>
<body>
<img id="theImage" src="http://31.media.tumblr./18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"></p>
</body>

for showing 5 images, you can use single button too. As per you requirement, you can create array of images sources and pass index on button click, as well as you can do the same which you were doing in your current snippet i.e passing source on button click.

Also, for single button what you can do is pass image src and find the index of that src index in array, from that index you change to next index and assign source to your image element. Please make sure to check if you are in last index than src at 0 index will get assigned, otherwise you will get stuck in error.

updated snippet: css display: inlne-block property will help you. also you need to change the width of image, because by default it

img {
  width:85%;
}
p {
  display: inline-block;
}
<script>
function pictureChange(path) {
console.log(path);
document.getElementById("theImage").src=path;
}
</script>
<body>
<img id="theImage" src="http://31.media.tumblr./18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"></p>
</body>

Now it's working, your "" were confusing the browser cause you can't nest with the same char, you must alternate " and ' when you nest

function pictureChange(path) {
console.log(path);
document.getElementById("theImage").src=path;
}
<img id="theImage" src="http://31.media.tumblr./18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"></p>

U wrong on use symbols ". Inside onclick function pictureChange u had symbol " then html throw syntax error.

u need use symbol ' changing for "

onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"

Good luck!

Current error so far with your code

onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"

You got confused with quote. Wrap with single quote if your outer quotes are double quotes.

currently only the single button. On button click I would like to show only the image that is associated with the button.

Just to make it more generic, pass the image id aswell.

function pictureChange(imgid,path) {
console.log(path);
document.getElementById(imgid).src=path;
}

And you can use it as

onclick="pictureChange('theImage','http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"
发布评论

评论列表(0)

  1. 暂无评论