使用功能翻转div [英] Flip divs using a function

查看:121
本文介绍了使用功能翻转div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想创建一个函数,并按下该按钮以反转两个div(第一个一个放在第二位,反之亦然)。我希望你明白。



我有这样的代码:

< div>文字1< / div>< div>文字2< / div><按钮>

解决方案

使用纯javascript,您可以简单地切换每个div的内容:

function flip(){div1_content = document.getElementById( DIV1\" )的innerHTML。 div2_content = document.getElementById(div2)。innerHTML; document.getElementById(div1)。innerHTML = div2_content; document.getElementById(div2)。innerHTML = div1_content;}

< div id =div1>文字1< / div>< div id =div2>文字2< / div><按钮onclick =flip()>翻转! < / button>

我们只是简单介绍每个div,然后将它们分配给另一个。希望你能接受它,从中学习,并运用它打算如何。


I'm new with javascript and I have a tiny problem.

I want to create a function and by pressing that button to invert the two divs ( the first one to be placed second and viceversa). I hope you understand.

I have this code:

<div> text 1 </div>
<div> text 2 </div>
<button> invert </button>

解决方案

Using pure javascript, you can simply switch the contents of each div:

function flip(){
  div1_content = document.getElementById("div1").innerHTML;
  div2_content = document.getElementById("div2").innerHTML;
  
  document.getElementById("div1").innerHTML = div2_content;
  document.getElementById("div2").innerHTML = div1_content;
}

<div id="div1"> Text 1 </div>
<div id="div2"> Text 2 </div>
<button onclick="flip()"> Flip it! </button>

We're simply storying the contents of each div, then assigning them to one another. Hopefully, you can take this, learn from it, and apply it how you intend to.

这篇关于使用功能翻转div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆