使用复选框交换div元素内容的位置 [英] Using checkbox to swap the position of content inside div element

查看:36
本文介绍了使用复选框交换div元素内容的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过JavaScript或JQuery切换内容吗?我有内容A和内容B,其中位置A紧挨着嫉妒而B位于右侧,当我点击复选框时,内容B将更改为左侧,内容A将更改为右侧,当我再次单击时,它将再次改为开头。

Can I switch the content via JavaScript or JQuery? I have content A and content B, where position A is next to envy and B is on the right, when I click the check box the content B will change to the left and content A to the right, and when I click again, it will change to like the beginning again.

这个我的片段,我尝试在A和B之间交换所有div内容。当我点击复选框时,会有所有div中的内容将与div B交换,但为什么只有输入形式发生变化?虽然内容没有变化,有人可以帮助我吗?我的代码有问题吗?

This my snippet, I tried exchanging all div content between A and B. When I clicked on the check box, there will be all content in div A will exchange with div B. but why does only the input form change? while the content doesn't change, can anyone help me? is there something wrong with my code?

function swap_content(conta,contb)
  {
    var tmp = document.getElementById(conta).value;
    document.getElementById(conta).value = document.getElementById(contb).value;
    document.getElementById(contb).value = tmp;
    var tdp = document.getElementById(text_id1).value;
    document.getElementById(text_id1).value = document.getElementById(text_id2).value;
    document.getElementById(text_id2).value = tdp;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="http://webapplayers.com/inspinia_admin-v2.8/css/bootstrap.min.css" rel="stylesheet">
<link href="http://webapplayers.com/inspinia_admin-v2.8/css/animate.css" rel="stylesheet">
<link href="http://webapplayers.com/inspinia_admin-v2.8/css/style.css" rel="stylesheet">


<body>
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-5" id="conta" style="background: red;">
                <div class="ibox ">
                    <div class="ibox-title">
                        <h5>
                            <input type="text" name="text_id1" id="text_id1" value="content A" >
                        </h5>
                    </div>
                    <div class="ibox-body">
                     <span style="color:white">
                     This is desc about Content A </span><br>

                     <img src="https://live.staticflickr.com/7254/7740405218_d3b9c5e839_h.jpg" width="100px" height="100px">
                 </div>
             </div>
         </div>

         <div class="col-lg-2">
            <div class="ibox ">
                <div class="ibox-title">

                    <div class="switch">
                        <div class="onoffswitch">
                            <input type="checkbox" checked class="onoffswitch-checkbox" id="example1" onclick="swap_content('text_id1','text_id2')">
                            <label class="onoffswitch-label" for="example1">
                                <span class="onoffswitch-inner"></span>
                                <span class="onoffswitch-switch"></span>
                            </label>
                        </div>
                    </div>

                </div>
            </div>
        </div>

        <div class="col-lg-5" id="contb" style="background: blue">
            <div class="ibox ">
                <div class="ibox-title">
                    <h5>
                        <input type="text" name="text_id2" id="text_id2" value="content B" >
                    </h5>
                </div>
                <div class="ibox-body">
                    <span style="color:white">This is desc about Content B</span> <br>

                    <img src="https://live.staticflickr.com/1429/5164748081_b2e7e19108_b.jpg" width="100px" height="100px">

                </div>
            </div>
        </div>

    </div>
</div>
<script src="http://webapplayers.com/inspinia_admin-v2.8/js/jquery-3.1.1.min.js"></script>
</body>

推荐答案

我建议使用 .click jquery事件监听器,如下所示。然后我使用 .html()来获取div的内容并交换内容。

I recommend using the .click jquery Event Listener to as I have done below. Then I used .html() to get the contents of the div and to swap the contents.

$('#example1').click(function() {
  var conta = $('#conta').html();
  var contb = $('#contb').html();
  $('#conta').html(contb);
  $('#contb').html(conta);
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="http://webapplayers.com/inspinia_admin-v2.8/css/bootstrap.min.css" rel="stylesheet">
<link href="http://webapplayers.com/inspinia_admin-v2.8/css/animate.css" rel="stylesheet">
<link href="http://webapplayers.com/inspinia_admin-v2.8/css/style.css" rel="stylesheet">


<body>
  <div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
      <div class="col-lg-5" id="conta" style="background: red;">
        <div class="ibox ">
          <div class="ibox-title">
            <h5>
              <input type="text" name="text_id1" id="text_id1" value="content A">
            </h5>
          </div>
          <div class="ibox-body">
            <span style="color:white">
                     This is desc about Content A </span><br>

            <img src="https://live.staticflickr.com/7254/7740405218_d3b9c5e839_h.jpg" width="100px" height="100px">
          </div>
        </div>
      </div>

      <div class="col-lg-2">
        <div class="ibox ">
          <div class="ibox-title">

            <div class="switch">
              <div class="onoffswitch">
                <input type="checkbox" checked class="onoffswitch-checkbox" id="example1">
                <label class="onoffswitch-label" for="example1">
                                <span class="onoffswitch-inner"></span>
                                <span class="onoffswitch-switch"></span>
                            </label>
              </div>
            </div>

          </div>
        </div>
      </div>

      <div class="col-lg-5" id="contb" style="background: blue">
        <div class="ibox ">
          <div class="ibox-title">
            <h5>
              <input type="text" name="text_id2" id="text_id2" value="content B">
            </h5>
          </div>
          <div class="ibox-body">
            <span style="color:white">This is desc about Content B</span> <br>

            <img src="https://live.staticflickr.com/1429/5164748081_b2e7e19108_b.jpg" width="100px" height="100px">

          </div>
        </div>
      </div>

    </div>
  </div>

</body>

这篇关于使用复选框交换div元素内容的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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