常见的前端效果

选中左右侧内容到另一侧

选中左侧内容到右侧,选中右侧内容到左侧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
select {
width: 100px;
height: 140px;
}

div {
width: 130px;
float: left;
text-align: center;
}
</style>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">

$(function(){
$("button:eq(0)").click(function(){
$("select[name=sel01] :selected").each(function(){
$(this).appendTo("select[name=sel02]");
});
});

$("button:eq(1)").click(function(){
$("select[name=sel01] option").each(function(){
$(this).appendTo("select[name=sel02]");
});
});

$("button:eq(2)").click(function(){
$("select[name=sel02] :selected").each(function(){
$(this).appendTo("select[name=sel01]");
});
});

$("button:eq(3)").click(function(){
$("select[name=sel02] option").each(function(){
$(this).appendTo("select[name=sel01]");
});
});
});
</script>
</head>
<body>
<div id="left">
<select multiple="multiple" name="sel01">
<option value="opt01">选项1</option>
<option value="opt02">选项2</option>
<option value="opt03">选项3</option>
<option value="opt04">选项4</option>
<option value="opt05">选项5</option>
<option value="opt06">选项6</option>
<option value="opt07">选项7</option>
<option value="opt08">选项8</option>
</select>

<button>选中添加到右边</button>
<button>全部添加到右边</button>
</div>
<div id="rigth">
<select multiple="multiple" name="sel02">
</select>
<button>选中删除到左边</button>
<button>全部删除到左边</button>
</div>
</body>
</html>

全选全不选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){

var $items = $(":checkbox[name=items]");

$("#checkedAllBtn").click(function(){
$items.add("#checkedAllBox").attr("checked",true);
});
$("#checkedNoBtn").click(function(){
$items.add("#checkedAllBox").attr("checked",false);
});
$("#checkedRevBtn").click(function(){
$(":checkbox[name=items]").each(function(){
this.checked = !this.checked;
});
$("#checkedAllBox").attr("checked",($items.filter(":checked").length == $items.length));
});
$("#checkedAllBox").click(function(){
$items.attr("checked",this.checked);
});
$items.click(function(){
$("#checkedAllBox").attr("checked",($items.filter(":checked").length == $items.length));
});
$("#sendBtn").click(function(){
$items.filter(":checked").each(function(){
alert(this.value);
});
});
});
</script>
</head>
<body>
<form method="post" action="">
你爱好的运动是?<input type="checkbox" id="checkedAllBox" />全选/全不选

<br />
<input type="checkbox" name="items" value="足球" />足球
<input type="checkbox" name="items" value="篮球" />篮球
<input type="checkbox" name="items" value="羽毛球" />羽毛球
<input type="checkbox" name="items" value="乒乓球" />乒乓球
<br />
<input type="button" id="checkedAllBtn" value="全 选" />
<input type="button" id="checkedNoBtn" value="全不选" />
<input type="button" id="checkedRevBtn" value="反 选" />
<input type="button" id="sendBtn" value="提 交" />
</form>
</body>
</html>

表单标签验证只能输入数字

1
<input name="" type="text" onkeyup="this.value=this.value.replace(/[^\d\+\-]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d\+\-]/g,'')"/>

四舍五入

小数点四舍五入保留两位,若为整数则小数点后补两个0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function changeTwoDecimal(v) {
if (isNaN(v)) {//参数为非数字
return 0;
}
var fv = parseFloat(v);
fv = Math.round(fv * 100) / 100; //四舍五入,保留两位小数
var fs = fv.toString();
var fp = fs.indexOf('.');
if (fp < 0) {
fp = fs.length;
fs += '.';
}
while (fs.length <= fp + 2) { //小数位小于两位,则补0
fs += '0';
}
return fs;
}

js退出循环

return false,return true, return的区别

1
2
3
4
5
6
7
js中:退出循环,使用break;退出当前循环继续下一个循环,使用continue;
jquery中的each()方法中要实现break,使用return false;continue,使用return true
return false 来阻止提交表单或者继续执行下面的代码,或者返回错误的处理结果

return true 返回正确的处理结果

return 终止当前函数的调用,返回给它的调用者。

delegate

1
2
$("#traveler").delegate("a[name='deleteTraveler']", "click", function(){
}

js等待2s后执行

1
2
$.jBox.tip(data.message, 'success');
window.setTimeout('window.location.href = "${ctx}/activity/list?showType=1"',300);

漂亮的表格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html>
<head>
<title>复杂点的表格样式</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
body {
width: 600px;
margin: 40px auto;
font-size: 14px;
color: #444;
}
table {
*border-collapse: collapse; /* IE7 and lower */
border-spacing: 0;
width: 100%;
}
.bordered {
border: solid #ccc 1px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 1px 1px #ccc;
-moz-box-shadow: 0 1px 1px #ccc;
box-shadow: 0 1px 1px #ccc;
}
.bordered tr:hover {
background: #fbf8e9;
-o-transition: all 0.1s ease-in-out;
-webkit-transition: all 0.1s ease-in-out;
-moz-transition: all 0.1s ease-in-out;
-ms-transition: all 0.1s ease-in-out;
transition: all 0.1s ease-in-out;
}
.bordered td, .bordered th {
border-left: 1px solid #ccc;
border-top: 1px solid #ccc;
padding: 10px;
text-align: left;
}
.bordered th {
background-color: #dce9f9;
background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
background-image: linear-gradient(top, #ebf3fc, #dce9f9);
-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset;
-moz-box-shadow:0 1px 0 rgba(255, 255, 255, .8) inset;
box-shadow: 0 1px 0 rgba(255, 255, 255, .8) inset;
border-top: none;
text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
}
.bordered td:first-child, .bordered th:first-child {
border-left: none;
}
.bordered th:first-child {
-moz-border-radius: 6px 0 0 0;
-webkit-border-radius: 6px 0 0 0;
border-radius: 6px 0 0 0;
}
.bordered th:last-child {
-moz-border-radius: 0 6px 0 0;
-webkit-border-radius: 0 6px 0 0;
border-radius: 0 6px 0 0;
}
.bordered th:only-child {
-moz-border-radius: 6px 6px 0 0;
-webkit-border-radius: 6px 6px 0 0;
border-radius: 6px 6px 0 0;
}
.bordered tr:last-child td:first-child {
-moz-border-radius: 0 0 0 6px;
-webkit-border-radius: 0 0 0 6px;
border-radius: 0 0 0 6px;
}
.bordered tr:last-child td:last-child {
-moz-border-radius: 0 0 6px 0;
-webkit-border-radius: 0 0 6px 0;
border-radius: 0 0 6px 0;
}
</style>
</head>
<body>
<table id="table" class="bordered">
<thead>
<tr>
<th>表头:第一列</th>
<th>表头:第二列</th>
<th>表头:第三列</th>
</tr>
</thead>
<tbody>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
<td>row 1, cell 3</td>
</tr>
<tr>
<td></td>
<td>row 2, cell 2</td>
<td>row 2, cell 3</td>
</tr>
<tr>
<td>row 3, cell 1</td>
<td>row 3, cell 2</td>
<td>row 3, cell 3</td>
</tr>
<tr>
<td colspan="2">合并2行</td>
<td>row 4, cell 3</td>
</tr>
</tbody>
</table>
</body>
</html>

显式效果:

0%