使用单选按钮更改图像
Change an image using radio buttons
我想弄清楚如何使用单选按钮将图像更改为不同的图像。我想使用 javascript 和 html/css 来实现这一点,但我的 javascript.
并不是那么完美
我知道这里还有其他答案,但它们似乎主要涉及 backgrounds/divs 和 jQuery,但我不太了解 jQuery。
这是我的代码:
HTML:
<form action="" id="animals">
<input type="radio" id="bunnybutton" name="animal" value="bunny" onClick="changeSrc()">bunny<br>
<input type="radio" id="kittybutton" name="animal" value="kitty" onClick="changeSrc()">kitty
</form>
<img src="https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg" id="picture" width=600px>
JAVASCRIPT:
function changeSrc() {
if (document.getElementById("bunnybutton").checked) {
document.getElementById("picture").src = "https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg";
} else if (document.getElementById("kittybutton").checked) {
document.getElementById("picture").src = "http://cutewallpaperss.com/wp-content/uploads/2015/01/cute_cats__cats_picture_cute_wallpaperss_hd_for_mobile.jpg";
}
}
在代码编辑器中,您的代码运行良好!
function changeSrc() {
if (document.getElementById("bunnybutton").checked) {
document.getElementById("picture").src = "https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg";
} else if (document.getElementById("kittybutton").checked) {
document.getElementById("picture").src = "http://cutewallpaperss.com/wp-content/uploads/2015/01/cute_cats__cats_picture_cute_wallpaperss_hd_for_mobile.jpg";
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<form action="" id="animals">
<input type="radio" id="bunnybutton" name="animal" value="bunny" onClick="changeSrc()">bunny<br>
<input type="radio" id="kittybutton" name="animal" value="kitty" onClick="changeSrc()">kitty
</form>
<img src="https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg" id="picture" width=600px>
我想弄清楚如何使用单选按钮将图像更改为不同的图像。我想使用 javascript 和 html/css 来实现这一点,但我的 javascript.
并不是那么完美我知道这里还有其他答案,但它们似乎主要涉及 backgrounds/divs 和 jQuery,但我不太了解 jQuery。
这是我的代码:
HTML:
<form action="" id="animals">
<input type="radio" id="bunnybutton" name="animal" value="bunny" onClick="changeSrc()">bunny<br>
<input type="radio" id="kittybutton" name="animal" value="kitty" onClick="changeSrc()">kitty
</form>
<img src="https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg" id="picture" width=600px>
JAVASCRIPT:
function changeSrc() {
if (document.getElementById("bunnybutton").checked) {
document.getElementById("picture").src = "https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg";
} else if (document.getElementById("kittybutton").checked) {
document.getElementById("picture").src = "http://cutewallpaperss.com/wp-content/uploads/2015/01/cute_cats__cats_picture_cute_wallpaperss_hd_for_mobile.jpg";
}
}
在代码编辑器中,您的代码运行良好!
function changeSrc() {
if (document.getElementById("bunnybutton").checked) {
document.getElementById("picture").src = "https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg";
} else if (document.getElementById("kittybutton").checked) {
document.getElementById("picture").src = "http://cutewallpaperss.com/wp-content/uploads/2015/01/cute_cats__cats_picture_cute_wallpaperss_hd_for_mobile.jpg";
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<form action="" id="animals">
<input type="radio" id="bunnybutton" name="animal" value="bunny" onClick="changeSrc()">bunny<br>
<input type="radio" id="kittybutton" name="animal" value="kitty" onClick="changeSrc()">kitty
</form>
<img src="https://pbs.twimg.com/profile_images/447374371917922304/P4BzupWu.jpeg" id="picture" width=600px>