分享人:王炳钧
1. jQuery 冒泡事件
<html>
<head>
<script type="text/javascript">
$(function() {
$('#clickMe').click(function() {
alert('hello');
});
$('body').click(function() {
alert('baby');
});
});
</script>
</head>
<body>
<div style="width:100px;height:100px;background-color:red;">
<button type="button" id="button2">
click me
</button>
<button id="clickMe">
click
</button>
</div>
</body>
</html>
事件冒泡现象:点击 “id=clickMe” 的button,会先后出现“hello” 和 “baby” 两个弹出框。
事件捕获现象:点击没有绑定点击事件的div和 “id=button2” 的button, 都会弹出 “baby” 的对话框。
阻止事件冒泡方法:
- 法1:
$('#clickMe').click(function() {
alert('hello');
return false;
});
- 法2:
$('#clickMe').click(function(event) {
alert('hello');
var e = window.event || event;
if (e.stopPropagation) { //如果提供了事件对象,则这是一个非IE浏览器
e.stopPropagation();
} else {
//兼容IE的方式来取消事件冒泡
window.event.cancelBubble = true;
}
});
事件捕获阻止方法:e.stopPropagation() e.preventDefault()
return false 不仅阻止了事件往上冒泡,而且阻止了事件本身。event.stopPropagation() 则只阻止事件往上冒泡,不阻止事件本身。
2. 父页面获取子页面数据
- 子页面:
function myReturnValue() {
var Datastrs = "";
$('#showTable>tr').each(function() {
var p_code = $(this).find('.p_code').text();
var leftTime = $(this).find('.leftTimeTable>input').val();
var rightTime = $(this).find('.rightTimeTable>input').val();
Datastrs += p_code + "," + leftTime + "," + rightTime + ";";
});
parent.callbackChangeTime(flightid, Datastrs.substring(0, Datastrs.length - 1));
parent.CloseAlertPage();
}
- 父页面:
//当前窗体
function editAlert(obj, totalHours, flightid) {
AlertPage("/CrewSupport/calculateTime.aspx?data=" + totalHours + "|" + flightid + "", "编辑左右座时间", 600, 400);
}
//窗体返回数据解析赋值
function callbackChangeTime(flightid, Datastrs) {
$('.' + flightid).find('.modifyDetail').text(Datastrs);
$('.detail_leftTime').text(0);
$('.detail_rightTime').text(0);
$('.modifyDetail').each(function() {
var text = $(this).text().split(';');
var DetailleftTime = 0;
var DetailrightTime = 0;
for (var i = 0; i < text.length; i++) {
var Arr = text[i].split(',');
var arrP_code = Arr[0];
var leftTime = Arr[1];
var rightTime = Arr[2];
var leftObj = $('#tblist_bottom').find('.' + arrP_code).find('.detail_leftTime').text();
var rightObj = $('#tblist_bottom').find('.' + arrP_code).find('.detail_rightTime').text();
$('#tblist_bottom').find('.' + arrP_code).find('.detail_leftTime').text(parseInt(leftObj) + parseInt(leftTime));
$('#tblist_bottom').find('.' + arrP_code).find('.detail_rightTime').text(parseInt(rightObj) + parseInt(rightTime));
}
});
}
如果用alertpage打开的页面,则用parent.父页面处理函数
如果用window.open打开的页面, 则用 window.opener.父页面处理函数
3. Json序列化与反序列化
当一个页面显示值来自多个表中时,并且通过ajax统一提交,同时更改数据库表值。此时可以用js遍历获取页面所有参数值,建立一个相对应的视图,将所有值拼接成为一个json串,然后再在后台反序列化为该视图集合,这样就可以在后台获取每一个值。
//反序列化
List<T>list = JsonConvert.DeserializeObject<List<T>>(jsonStr);
//序列化
string jsonStr = JsonConvert.SerializeObject(list);
result = result + "[{";
result += "\"PLAN_DEPARTURE\":\"" + plan_departure + "\",\"PLAN_ARRIVAL\":\"" + plan_arrival
+ "\",\"FLIGHT_ID\":\"" + flight_id + "\",\"FLIGHT_TYPE\":\"" + flight_type
+ "\",\"LAUNCH_LEFT_CODE\":\"" + launch_left_code + "\",\"LAUNCH_RIGHT_CODE\":\""
+ launch_right_code + "\",\"LANDING_LEFT_CODE\":\"" + landing_left_code
+ "\",\"LANDING_RIGHT_CODE\":\"" + landing_right_code
+ "\",\"LAUNCH_OP_CODE\":\"" + launch_op_code + "\",\"LANDING_OP_CODE\":\"" + landing_op_code
+ "\",\"APPROACH_TYPE\":\"" + approach_type + "\",\"TAXI_OUT_TIME\":\"" + taxi_out_time
+ "\",\"TAKEOFF_TIME\":\"" + takeoff_time + "\",\"LANDING_TIME\":\"" + landing_time
+ "\",\"TAXI_IN_TIME\":\"" + taxi_in_time + "\",\"AIR_HOURS\":\"" + air_hours + "\",\"GRND_HOURS\":\""
+ grnd_hours + "\",\"TOTAL_HOURS\":\"" + total_hours + "\",\"NIGHT_HOURS\":\"" + night_hours
+ "\",\"OLD_FUEL\":\"" + old_fuel + "\",\"NEW_FUEL\":\"" + new_fuel + "\",\"LEFT_FUEL\":\"" + left_fuel
+ "\",\"TRIP_FUEL\":\"" + trip_fuel + "\",\"WHOLE_WEIGHT\":\"" + whole_weight
+ "\" ,\"MODIFY_DETAIL\":\"" + modifyDetail + "\",\"FLIGHT_DATE\":\"
<%=flightDate.ToString()%>\",\"IS_ENTER\":\"" + is_enter + "\",\"FLIGHT_NO\":\"" + flight_no + "\"";
result = result + "},";