用一个按钮交换div内容

时间:2017-08-18 14:25:00

标签: javascript jquery html css

我在每个div中有两种类型的内容。一个是表,另一个是表。我想通过按下按钮来交换它。

HTML

<a id="btn" class="btn btn-primary pull-right"><i class="fa fa-envelope"> Contact Us</i></a>
<br style="clear:both" />
<div class="well well-sm">
  <div id="table">
    <table class="table table-bordered">
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Price</th>
      </tr>
      <tr>
        <td>001</td>
        <td>DL650XT</td>
        <td>369000</td>
      </tr>
    </table>
  </div>
  <div id="formDiv">
    <form class="form" method="post" action="" name="contact" id="contact">
      <fieldset>
        <legend> Contact Form </legend>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
            <input type="text" class="form-control" id="name" name="name" placeholder="Name *" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
            <input type="text" class="form-control" id="email" name="email" placeholder="E-Mail *" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
            <input type="text" class="form-control" id="telefon" name="telefon" placeholder="Telefon" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
            <textarea type="text" id="message" class="form-control" name="message" rows="10" cols="40" placeholder="Nachricht*"></textarea>
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group">
          <button type="submit" id="submit" class="btn btn-primary btn-lg">Submit</button>
          <button type="reset" id="cancel" class="btn btn-danger btn-lg">Reset 1</button>
        </div>

      </fieldset>
    </form>
  </div>
</div>

的Javascript

$(document).ready(function() {
      $('#formDiv').css('border', 'red !important');
      //swap content
      $('#btn').on('click',function() {
        $('#table').hide();
        $('#formDiv').prop('display', 'show');
      })
    });

它只工作一次。我希望每次点击都能交换内容。

这是小提琴:https://jsfiddle.net/krm85qq3/5/

3 个答案:

答案 0 :(得分:1)

您可以使用toggle功能显示隐藏 匹配的元素。

$('#btn').on('click',function() {
    $('#table').toggle();
    $('#formDiv').prop('display', 'show');
})

$(document).ready(function() {
      $('#btn').on('click', function() {
        $('#table').toggle();
        $('#formDiv').toggle();
      });
    });
#formDiv {
  display: none;
  border: red;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="btn" class="btn btn-primary pull-right"><i class="fa fa-envelope"> Contact Us</i></a>
<br style="clear:both" />
<div class="well well-sm">
  <div id="table">
    <table class="table table-bordered">
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Price</th>
      </tr>
      <tr>
        <td>001</td>
        <td>DL650XT</td>
        <td>369000</td>
      </tr>
    </table>
  </div>
  <div id="formDiv">
    <form class="form" method="post" action="" name="contact" id="contact">
      <fieldset>
        <legend> Contact Form </legend>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
            <input type="text" class="form-control" id="name" name="name" placeholder="Name *" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
            <input type="text" class="form-control" id="email" name="email" placeholder="E-Mail *" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
            <input type="text" class="form-control" id="telefon" name="telefon" placeholder="Telefon" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
            <textarea type="text" id="message" class="form-control" name="message" rows="10" cols="40" placeholder="Nachricht*"></textarea>
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group">
          <button type="submit" id="submit" class="btn btn-primary btn-lg">Submit</button>
          <button type="reset" id="cancel" class="btn btn-danger btn-lg">Reset 1</button>
        </div>

      </fieldset>
    </form>
  </div>
</div>

答案 1 :(得分:0)

您可以使用jQuery {{1}}函数自动在显示/隐藏状态之间切换:)

答案 2 :(得分:0)

切换效果很好,但您也可以手动处理。例如,如果您希望能够在联系表单显示时与表格显示时执行某些功能。以下代码执行与切换相同的操作,仅手动应用。只是为了变化。 :)

$(document).ready(function() {
  var formEl = $("#formDiv"),
      tableEl = $("#table"),
      btnEl = $("#btn");

      //swap content
      btnEl.on('click',function() {
        if (tableEl.is(":visible")) {
          tableEl.hide();
          formEl.show();
          btnEl.find("i").text(" Return to Table");
        } else {
          tableEl.show();
          formEl.hide();
          btnEl.find("i").text(" Contact Us");
        }
      })
    });
#formDiv {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"/>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<a id="btn" class="btn btn-primary pull-right"><i class="fa fa-envelope"> Contact Us</i></a>
<br style="clear:both" />
<div class="well well-sm">
  <div id="table">
    <table class="table table-bordered">
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Price</th>
      </tr>
      <tr>
        <td>001</td>
        <td>DL650XT</td>
        <td>369000</td>
      </tr>
    </table>
  </div>
  <div id="formDiv">
    <form class="form" method="post" action="" name="contact" id="contact">
      <fieldset>
        <legend> Contact Form </legend>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
            <input type="text" class="form-control" id="name" name="name" placeholder="Name *" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
            <input type="text" class="form-control" id="email" name="email" placeholder="E-Mail *" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
            <input type="text" class="form-control" id="telefon" name="telefon" placeholder="Telefon" />
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group has-feedback">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
            <textarea type="text" id="message" class="form-control" name="message" rows="10" cols="40" placeholder="Nachricht*"></textarea>
            <span class="form-control-feedback glyphicon"></span>
          </div>
        </div>

        <div class="form-group">
          <button type="submit" id="submit" class="btn btn-primary btn-lg">Submit</button>
          <button type="reset" id="cancel" class="btn btn-danger btn-lg">Reset 1</button>
        </div>

      </fieldset>
    </form>
  </div>
</div>

在上面的示例中,如果正在显示该表并且您单击该按钮,我还想更改btn el的文本。因此,知道表el的状态并将其用作我的手动切换点,我可以随意修改其他元素的状态。