首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过单击按钮更改图像的顺序

通过单击按钮更改图像的顺序
EN

Stack Overflow用户
提问于 2014-10-31 19:52:00
回答 2查看 1.1K关注 0票数 0

帮我解决这个琐碎的问题。

我需要在button_Click中更改图像顺序。我该怎么做呢?

实际上,我需要**最简单**的方式,**没有jQuery**。

将非常感谢一段工作的代码和解释-这是一个令我头疼的问题

以下是我的代码

另外,我已经得到了一些建议,但它们都连接到了jQuery

代码语言:javascript
运行
复制
No jQuery
代码语言:javascript
运行
复制
<div id="one"> 
              
        <input id="b1" value="Change the image order" onclick="change_order()" type="button"/>
       
        <h1 style="font-size: 12"> Header HeaderHeaderHeader</h1> 
        <p style="font-size:8"> text text text </p>
    </div>


<div id="picContainer">
        
        <img id="pict_01" src="http://heaversfarm.files.wordpress.com/2013/01/i-love-maths.jpg" />
        <img id="pict_02" src="http://www.uplandsoutreach.org/wp-content/uploads/2013/04/20maths1.jpg" />
        <img id="pict_03" src="http://www.milldamschool.ik.org/img/d666f5fc-db14-11de-a689-0014220c8f46-5812526.jpg" />
        <img src="http://www.birdsontheblog.co.uk/wp-content/uploads/2010/05/maths.jpg" />
        
    </div>

EN

回答 2

Stack Overflow用户

发布于 2014-10-31 20:50:49

希望你发现下面的代码按照你的要求工作。

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click me</button><br/>
<br/>
<img height="50" width="50" id="demo1" src="http://www.largeyellowbutton.com/largeyellowbutton.jpg"></img>
<br/>
<img height="50" width="50" id="demo2" src= "http://libn.com/wp-files/graphics/register-button_0.jpg"></img>
<br/>
<img height="50" width="50" id="demo3" src=    "http://mygimptutorial.com/images/button-with-reflection/11.jpg"></img>

<script>
 function myFunction() {
 var temp=document.getElementById("demo1").src;
 document.getElementById("demo1").src = document.getElementById("demo2").src;
 document.getElementById("demo2").src = document.getElementById("demo3").src;
 document.getElementById("demo3").src = temp;
 }
</script>

</body>
</html>
票数 0
EN

Stack Overflow用户

发布于 2014-10-31 21:21:11

使用我在上面发布的代码,以防您知道div中的图像数量。如果您不确定要添加的图像数量,请使用以下代码。

代码语言:javascript
运行
复制
<html>
<head>
<script>
function myFunction() {
var count=document.getElementById("picContainer").getElementsByTagName("img").length;
//below code captures the first image source before 
//changing the image order. This is also used to assign as source to the
// last image tag.
var temp=document.getElementById("pict_01").src;
var i;

for(i=1;i<=count;i++)
{
 //If value of i is equal to count then, assign the first image source
 // captured in temp variable to the last image inside the div.
 if(i==count){
 document.getElementById("pict_0"+i).src=temp;
 }

//below code assigns image in decreasing order.
document.getElementById("pict_0"+i).src = document.getElementById("pict_0"+(i+1)).src;
}
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value=" Change an image order"></input> 
<br/>
<div id="picContainer">
    <img id="pict_01" src="http://heaversfarm.files.wordpress.com/2013/01/i-love-maths.jpg" />
    <img id="pict_02" src="http://www.uplandsoutreach.org/wp-content/uploads/2013/04/20maths1.jpg" />
    <img id="pict_03" src="http://www.milldamschool.ik.org/img/d666f5fc-db14-11de-a689-0014220c8f46-5812526.jpg" />
    <img id="pict_04" src="http://www.birdsontheblog.co.uk/wp-content/uploads/2010/05/maths.jpg" />
</div>
</body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26674094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档