将输入大小调整为跨度大小

时间:2016-08-20 14:31:09

标签: javascript jquery html css

我有各种跨度变成输入框,所以我可以编辑它们,每次按特定按钮。我的问题是CSS,它改变了表格的整个布局。

我该如何解决这个问题,因此表格行不会改变,输入框的宽度和高度与字母的宽度和高度完全相同?

这是我的小提琴:

FIDDLE

这是HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">    </script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<table>
  <tr>
    <th  class="col-xs-6">Name</th>
    <th  class="col-xs-2" >Value</th>
  </tr>
  <tr>
    <td><span id="s1" class="editable-span col-xs-6">John</span>
    </td>
    <td>
      <span id="s2" class="editable-span col-xs-2">2345</span>
    </td>
  </tr>
  <tr>
    <td>
      <span id="s3" class="editable-span col-xs-2">Dave</span>
    </td>
    <td>
      <span id="s4" class="editable-span col-xs-2">5678</span>
    </td>
  </tr>
  <tr>
    <td>
      <span id="s5" class="editable-span col-xs-12 col-md-8">Sarah</span>
    </td>
    <td><span id="s6" class="editable-span col-xs-2">1987</span></td>
  </tr>
</table>
<button id="edit_properties" class="col-xs-2">Edit</button>
<button id="unedit_properties" class="col-xs-2">Save</button>

JavaScript:

$("#edit_properties").on("click", function() {
$('.editable-span').replaceWith(function() {
return $("<input>", {
  val: $(this).text(),
  type: "text",
  id: this.id,
  class: 'editable-input'
  });
 });
});

$("#unedit_properties").on("click", function() {
$('.editable-input').replaceWith(function() {
return $("<span>", {
  text: this.value,
  id: this.id,
  class: 'editable-span'
  });
 });
});

2 个答案:

答案 0 :(得分:1)

保存时缺少“col-xs-6”类:

$("#edit_properties").on("click", function() {
  $('.editable-span').replaceWith(function() {
    return $("<input>", {
      val: $(this).text(),
      type: "text",
      id: this.id,
      class: 'editable-input'
    });
  });
});

$("#unedit_properties").on("click", function() {
  $('.editable-input').replaceWith(function() {
    return $("<span>", {
      text: this.value,
      id: this.id,
      class: 'editable-span'
    });
  });
});

更新了小提琴:http://jsfiddle.net/ersamrow/2D9FW/197/

答案 1 :(得分:-1)

尝试修复跨度和输入的高度和宽度---

span
{
width:100px;
height:20px;
padding:2px;}
input
{
width:100px;
height:20px;
padding:2px;
}