JavaScript/CSS : Change button to a text field on click?

标签 javascript html css

我有一个按钮,我需要在单击时将其更改为文本字段(理想情况下使用动画将其展开)。现在我的按钮和文本字段在页面上彼此相邻:

echo '<form id= "changePassForm" action="" method="post" enctype="multipart/form-data">';  
echo '<div class="changePass">Change Password</div>';
echo '<div class="changePassBtn" method="post"></div>';
echo '<input type="password" placeholder="Password" name="password">';
echo '</form>';  

我可以将密码设置为在开始时隐藏,但我需要知道如何使用 CSS 或 javascript 使 changePass 按钮“变成”或扩展到密码文本字段。 我是 javascript 的新手,不确定如何在单击按钮时完成此操作。

如何在单击时将按钮更改为文本字段?之后我需要能够改回按钮

我得到这个: enter image description here

有了这个:

echo '<form id="changePassForm" action="" method="post" enctype="multipart/form-data">
  <div class="changePass">
    <div class="changePassBtn">Change Password</div>
    <input type="password" placeholder="Password" name="password">
  </div>';

    .wrapper {
        background: #50a3a2;
        background: -webkit-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
        background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%);
        position: absolute;
        top: 0%;
        left: 0;
        width: 100%;
        height: 100%;
        margin-top: 0px;
        overflow: scroll;
        z-index: -1;
        text-align: center;
    }
.changePass {
  position: relative;
  width: 200px;
  height: 1px;
}

.changePass input {
  position: absolute;
  padding: 5px;
  display: none;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;

}

.changePass, .changePassBtn {
  background: #2d89ef;
  border-bottom: 2px solid #2b5797;
  color: white;
  padding: 4px;
  display: inline;
  border-radius: 2px;
  position: absolute;
  overflow: hidden;
  white-space: nowrap;
  text-align: center;
  left: 0;
  right: 0;
  margin-left: auto;
  margin-right: auto;
  width: 120px;
}

.changePass, .changePassBtn:hover {
  background: #2b5797;
  border-top: 2px solid #2d89ef;
  border-bottom: 0;
}

即使我把它完全从 wrapper 里拿出来,你的颜色也看不出来。为什么会这样?

最佳答案

将您的按钮和输入字段放在同一个 div 中,然后使用 relativeabsolute 的定位技巧将它们重叠。然后使用 JQuery 使其在隐藏元素时看起来很漂亮。

JSFiddle:https://jsfiddle.net/wpbL3kjx/9/

//Shows Input Box When Focussed
$(".changePassBtn").click(function() {
  var neww = $(".changePass input").css("width");
  $(this).animate({
    width: neww
  }, 300, function() {
    $(".changePass input").fadeIn(300, function() {
      $(".changePassBtn").hide();
    }).focus();
  });
});

//Shows Button When Unfocussed
$(".changePass input").blur(function() {
  $(".changePassBtn").css("width", "auto");
  var neww = $(".changePassBtn").css("width");
  $(this).animate({
    width: neww
  }, 300, function() {
    $(".changePassBtn").show(0, function() {
      $(".changePass input").fadeOut(500, function() {
        $(".changePass input").css("width", "auto");
      });
    });
  });
});
.changePass {
  position: relative;
}
.changePass input {
  position: absolute;
  padding: 5px;
  display: none;
}
.changePass .changePassBtn {
  background: #2d89ef;
  border-bottom: 2px solid #2b5797;
  color: white;
  padding: 4px;
  display: inline;
  border-radius: 2px;
  position: absolute;
  overflow: hidden;
  white-space: nowrap;
}
.changePass .changePassBtn:hover {
  background: #2b5797;
  border-top: 2px solid #2d89ef;
  border-bottom: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="changePassForm" action="" method="post" enctype="multipart/form-data">
  <div class="changePass">
    <div class="changePassBtn">Change Password</div>
    <input type="password" placeholder="Password" name="password">
  </div>
</form>

关于JavaScript/CSS : Change button to a text field on click?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37284258/

相关文章:

javascript - 我在 ng-keyup 上调用一个函数在数据库中搜索,得到结果后我在 DIV 中显示结果..?

angularjs - 网络核心。有 Angular 的。静态页面和html5模式

jquery - 使内部div比外部div宽

javascript - navigator.clipboard.readText() 在 Firefox 中不起作用

javascript - 在 JavaScript 中使用单个数组更新数组对象

Javascript:如何将 console.log 结果返回为字符串形式?

javascript - 从弹出窗口发送消息后未收到后台响应

css - float 跨度增加了IE中div的宽度

css - 填充屏幕宽度

jquery - Rails 4 - jQuery 动画旋转 - 几乎可以正常使用