我從數據庫表中拉出信息並將其放置到HTML表中。我想讓用戶點擊表格中的一個數字並且能夠編輯它。我用jquery做點擊事件,但是當我使用<textarea>
時,它擴大了單元格並且弄亂了整個桌子的外觀。我試過<input>
,它也是這樣。任何方式,表格單元本身可以變成一個輸入?這裏有兩張截圖,第一張是表格,第二張截圖是爲了顯示添加輸入時表格如何偏斜。
Screenshot of Table
Screenshot of Table with Textarea使HTML表元素</td>可編輯
的問題是,什麼是編輯文本我最好的選擇。 <textarea>
使細胞更大,說實話,它看起來很醜陋... contenteditable
無法與IE瀏覽器的聲音一起使用,因此無法將其作爲選項使用。我的最終目標是在輸入數據後也將值保存回數據庫,這就是爲什麼我使用jquery進行點擊的原因,因此添加更多代碼以便將文本返回到數據庫後很容易。
<?php
include("connection.php");
$query= "SELECT * FROM schedule";
$result = mysqli_query($link, $query);
$scheduletext="<table>";
if($result = mysqli_query($link, $query)) {
$b=1;
while ($row=mysqli_fetch_array($result)) {
$scheduletext.="<tr>";
$a=1;
while($a< mysqli_num_fields($result)) {
if(($a == 1)and ($b == 1)){
$scheduletext.="<th></th>";
} else {
if (($a == 1) or ($b == 1)) {
$scheduletext.="<th>".$row[$a]."</th>";
} else {
$scheduletext.="<td>".$row[$a]."</td>";
}
}
$a++;
}
$scheduletext.="</tr>";
$b++;
}
}
$scheduletext=$scheduletext."</table>";
?>
<html>
<head>
<title>TastySnack - Production Schedule</title>
<link href="https://fonts.googleapis.com/css?family=Kaushan+Script" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="tasty.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="top">
<div id="top-left">
TastySnack Production
</div>
<div id="top-right">
<img id="logo" src="images/TastysnackLogo.jpg">
</div>
</div>
<div id="split"></div>
<div id="schedule">
<?php
print_r($scheduletext);
?>
<div id="new"></div>
</div>
<script type="text/javascript">
$("td").click(function(){
$(this).html("<textarea>");
});
</script>
</body>
</html>
請提供實際的代碼,而不是截圖。 –
'td'和'th'是'tr'中唯一允許的子元素。另請閱讀https://stackoverflow.com/help/how-to-ask。 – connexo
是什麼問題? –