เพื่อที่จะตอบคำถามในช่วงสุดสัปดาห์ฉันเขียนโค้ดจำนวนมากและฟังก์ชั่นนั้นถูกนำไปใช้โดยทั่วไป แต่ฉันรู้สึกว่าโครงสร้างนั้นแย่เกินไปและฉันก็ไม่ได้คิดมาก ไม่สวยเลย
การคัดลอกรหัสมีดังนี้:
<! doctype html>
<html>
<head>
<title> ชื่อเอกสาร </title>
<meta charset = utf-8>
<script src = "lib/jQuery-1.8.1.min.js" type = "text/javascript"> </script>
<style type = "text/css">
โต๊ะ{
ระยะห่างชายแดน: 0;
-
td {
ความสูง: 50px;
ความกว้าง: 50px;
ระยะห่างชายแดน: 0;
-
.ที่ซ่อนอยู่{
แสดง: ไม่มี;
-
</style>
</head>
<body>
<table>
<tr id = "r1">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr id = "r2">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<p id = 'log'> </p>
<button type = "button" onclick = "combine ()"> hebing </button>
<script type = "text/javascript">
var selectecdlog = [];
var resultlen = 0;
var resultBegin = 0;
var backcolor = ['สีแดง', 'สีเขียว', 'สีน้ำเงิน']; // สมมติว่า 3 แถว 1 -Red 2 - Green 3 - Blue
var resultColor = 0;
$ ("td"). mousedown (function () {
selectecdlog = [];
resultColor = backColor [$ (this) .parent (). attr ('id'). substr (1) -1];
$ ('td'). css ('พื้นหลังสี', 'สีขาว');
// id สำหรับแท็ก TR
//alert($( นี่). parent().attr('id '));
// รับหมายเลขคอลัมน์
//alert($( นี่).index()+1);
$ (นี่) .css ('พื้นหลังสี', ผลลัพธ์สี);
selectecdlog.push ($ (นี่) .attr ('id'));
//alert($( นี่).attr('id '));
$ ("TD"). MouseUp (onMouseUp);
$ ("TD") MouseOver (OnMouseOver);
-
ฟังก์ชั่น onmouseup () {
/ * ดำเนินการกับเหตุการณ์ */
var len = resultlen = math.abs (parseint (selectecdlog [0] .substr (selectecdlog [0] .indexof ('c')+1))) - parseint ($ (นี่) .attr ('id') substr ($ (นี้)
VAR เริ่มต้น = resultBegin = parseInt (selectecdlog [0] .substr (selectecdlog [0] .indexof ('c')+1))> parseint ($ (นี้) .attr ('id'). substr (($ (นี้) .attr ('id') parseint ($ (นี่) .attr ('id'). substr (($ (นี้) .attr ('id'). indexof ('c'))+1));
สำหรับ (var i = 0; i <resultlen; i ++)
-
selectecdlog.push (selectecdlog [0] .substr (0,2)+"c"+(resultbegin+i));
-
selectecdlog.shift ();
$ ('#log') ข้อความ (selectecdlog.toString ());
$ ("TD"). Unbind ('MouseOver', OnMouseOver);
$ ("TD"). Unbind ('Mouseup', onmouseup);
-
ฟังก์ชั่น onmouseover (อาร์กิวเมนต์) {
var len = resultlen = math.abs (parseint (selectecdlog [0] .substr (selectecdlog [0] .indexof ('c')+1))) - parseint ($ (นี่) .attr ('id') substr ($ (นี้)
VAR เริ่มต้น = resultBegin = parseInt (selectecdlog [0] .substr (selectecdlog [0] .indexof ('c')+1))> parseint ($ (นี้) .attr ('id'). substr (($ (นี้) .attr ('id') parseint ($ (นี่) .attr ('id'). substr (($ (นี้) .attr ('id'). indexof ('c'))+1));
$ ('td'). css ('พื้นหลังสี', 'สีขาว');
สำหรับ (var i = 0; i <len; i ++)
-
$ ('#'+selectecdlog [0] .substr (0,2)+"C"+(เริ่มต้น+i)). CSS ('พื้นหลังสี', ผลลัพธ์สี);
-
// Alert ($ (นี่) .attr ('id'). substr (($ (นี้) .attr ('id'). indexof ('c'))+1));
//$('#'+tdid).css('background-Color ',' Red ');
-
// ตั้งค่า id attr สำหรับแต่ละ td
$ ("td"). แต่ละ (ฟังก์ชั่น (ดัชนี, val) {
/ * วนซ้ำผ่านอาร์เรย์หรือวัตถุ */
$ (นี่) .attr ('id', $ (this) .parent (). attr ('id')+"c"+($ (นี่) .index ()+1))});
// รวมเซลล์
ฟังก์ชั่นรวม (อาร์กิวเมนต์) {
// ร่างกาย...
สำหรับ (var m ใน selectecdlog)
-
ถ้า (m == 0)
-
$ ('#'+selectecdlog [m]). attr ('colspan', resultlen);
$ ('#'+selectecdlog [m]). css ('ความกว้าง', resultlen*50+"px");
//$('#'+Selectecdlog [เหมือนกันนะ).css('background-Color ',' Bule ');
-
อื่น{
$ ('#'+selectecdlog [m]). addClass ('ซ่อน');
-
-
-
</script>
</body>
</html>