admin管理员组文章数量:1406034
I'm new to Javascript and I am trying to simplify my life by integrating many functions into one.
I have a very simple operation which allows for the click of a button to load a specific image in place of a default image. As oppose to having many functions which do the same thing, I'd rather have just one.
I imagine you can store the images in an array and select them by position?
Here is what I have so far.
function swap1() {
document.getElementById("default").src="321.jpg";
}
function swap2() {
document.getElementById("default").src="432.jpg";
}
function swap3() {
document.getElementById("default").src="742.jpg";
}
<input type="button" onClick="swap1()">
<input type="button" onClick="swap2()">
<input type="button" onClick="swap3()">
I'm new to Javascript and I am trying to simplify my life by integrating many functions into one.
I have a very simple operation which allows for the click of a button to load a specific image in place of a default image. As oppose to having many functions which do the same thing, I'd rather have just one.
I imagine you can store the images in an array and select them by position?
Here is what I have so far.
function swap1() {
document.getElementById("default").src="321.jpg";
}
function swap2() {
document.getElementById("default").src="432.jpg";
}
function swap3() {
document.getElementById("default").src="742.jpg";
}
<input type="button" onClick="swap1()">
<input type="button" onClick="swap2()">
<input type="button" onClick="swap3()">
Share
Improve this question
edited Jul 27, 2013 at 22:59
Dennis
59.7k27 gold badges148 silver badges142 bronze badges
asked Jul 27, 2013 at 22:42
dpalmdpalm
1431 gold badge3 silver badges17 bronze badges
2 Answers
Reset to default 6use function parameters:
function swap(imgNumber)
{
document.getElementById("default").src=imgNumber+".jpg";
}
and later:
<input type="button" onclick="swap('321')">
<input type="button" onclick="swap('432')">
<input type="button" onclick="swap('742')">
To answer the ment (not very best js code but to show idea of variables):
var clip=false;
function setClip(val) {
clip=val;
}
function swap(imgNumber)
{
if(clip==true)
document.getElementById("default").src=imgNumber+"clip.jpg";
else
document.getElementById("default").src=imgNumber+".jpg";
}
<input type="button" onclick="swap('321')">
<input type="button" onclick="swap('432')">
<input type="button" onclick="swap('742')">
<input type="button" onclick="setClip(true)">
Simply you add variable which takes true/false - last button set clip
variable to true
- we changed swap function to check if clip
is true - if yes - it loads different file
You could do this
JS
function swap(string img)
{
document.getElementById("default").src=img;
}
HTML
<input type ="button" onClick="swap('321.jpg')">
<input type ="button" onClick="swap('432.jpg')">
<input type ="button" onClick="swap('742.jpg')">
本文标签: Using buttons to swap image Javascript htmlStack Overflow
版权声明:本文标题:Using buttons to swap image Javascript html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744967025a2635009.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论