[英]Adjusting size of input to size of span
我有各種跨度都可以變成輸入框,因此每次我按特定按鈕時都可以編輯它們。 我的問題是CSS,它會更改表格的整個布局。
我該如何解決這個問題,使表格的行不發生變化,並且輸入框的寬度和高度與跨度的輸入框完全相同?
這是我的小提琴:
這是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'
});
});
});
保存時缺少類“ 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/
嘗試確定跨度和輸入的高度和寬度
span
{
width:100px;
height:20px;
padding:2px;}
input
{
width:100px;
height:20px;
padding:2px;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.