更多的开发经验

//合并所有边框
<table> style="border: solid; border-collapse: collapse;"
//统计表中最大的行数,即可获得表中有多少数据
"select max(rownum) from t_paper"
//<div>让div跟随滚动条滚动
position: fixed;
//登录框输入中文传到后台乱码解决办法
String stu_name = new String(req.getParameter("stu_name").getBytes("ISO-8859-1"),"utf-8");
============================================================================================================================================
//HashMap的遍历方法
HashMap<String, Session> socketMap = new HashMap<String, Session>();
Set<String> keys = socketMap.keySet();
Iterator it= keys.iterator();
while(it.hasNext()){
String key = (String) it.next();
Session temp = socketMap.get(key);
temp.get×××
}
============================================================================================================================================
//符合JSON格式的字符串转JSON对象
String str;
json = JSON.parse(str)
============================================================================================================================================
//倒计时方法:
var time = parseInt(str.time);//得到开始的时间(此时间相当于getTime())并转成INT型
var $test_t = $(".test_time").text(); //获得实际的考试时间
var test_time = parseInt($test_t);//将考试时间转换成整型
var time_sec = parseInt(test_time*60*1000);//考试时间转换成毫秒值
var end_time = time+time_sec;//得到结束考试的时间毫秒值
if(type == "start_exam"){
setInterval(function() {
var date = new Date();//获得当前系统时间
var date_time = parseInt(date.getTime());//获得当前系统时间毫秒值
var sec = Math.floor((end_time - date_time)/1000);//结束的时间减去当前时间得到剩余的倒计时时间
var hour = 0;
var min = 0;
if(sec > 60){
min = sec/60;
sec = sec%60;
}
if(min > 60){
hour = min/60;
min = min%60;
}
sec = parseInt(sec);//调用parseInt()方法将小数点后的数字去掉
min = parseInt(min);
hour = parseInt(hour);
if(sec < 10){
sec = "0"+sec;
}
if(min < 10){
min = "0"+min;
}
if(hour < 10){
hour = "0"+hour;
}
$("#end_time").text(hour+":"+min+":"+sec);
-----------------------------------------------------------------------------------------------------------------------------------------------
//计时器
var index = 0;
function setTime(){
var second = parseInt(index % 60);
var minute = parseInt((index % 3600) / 60);
var hour = parseInt(index / 3600);
second = second<10? "0"+second : second;
minute = minute<10? "0"+minute : minute;
hour = hour<10? "0"+hour : hour;
$("#showTime").html(hour+":"+minute+":"+second);
index++;
}
============================================================================================================================================
//JS中字符串转Int型
1、Number()比较繁琐,相对不合理;
2、parseInt()最为常用,能够过滤掉非数字字符,如123blue会转换成123;
3、parseFloat()与第二点类似,但是这个函数对第一个小数点有效,第二个小数点后无效;
============================================================================================================================================
//获得某标签某属性的值
var $num = $("#111").attr("属性名");
============================================================================================================================================
//WebSocket带参方法
客户端:
websocket = new WebSocket("ws://localhost:9090/TheOSTA/wait/参数");
服务器:
@ServerEndpoint("/wait/{num}")//路径带参
private static CopyOnWriteArraySet<WaiteToExam> webSocketSet = new CopyOnWriteArraySet<WaiteToExam>();
private static HashMap<String, Session> socketMap = new HashMap<String, Session>();
@OnOpen
public void onOpen(@PathParam("num")String num, Session session){
this.session = session;
this.num = num;
webSocketSet.add(this);
socketMap.put(num, session);//键值对储存session
}
============================================================================================================================================
//根据字符拆解字符串
var str;
var result = str.split("?")[index];
============================================================================================================================================
//回去看fileupLoad
============================================================================================================================================
//使用过滤器前,将过滤器的req resp强转为HttpServlet
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
============================================================================================================================================
//点击事件,改变某标签的ID等属性值的方法
$(function(){
$("#stop").click(function(){//原本的ID是stop
if($(this).attr("id") == "stop"){
var stop = '{"type": "stop_exam"}';
websocket.send(stop);
$(this).text("继续考试");
$(this).attr("id", "go_on");//改变属性的方法
clearInterval(runTime);
}else{
alert($("#end_time").text());
$(this).text("暂停考试");
$(this).attr("id", "stop");
}
});
});
============================================================================================================================================
//prop用来处理标签固有属性,attr则处理自定义的属性
对于复选框改变其是否选择的属性,最好使用prop,这样能保证设置有效,如果使用attr则第一次有效,之后无效
============================================================================================================================================
//JS当中向数据增加或删除数据
var arr = new Array();
arr .push($num);//向数组添加一个数据
arr .splice($num);//向数据删除一个数据
============================================================================================================================================
//JQ当中$.each()的用法
$("input[id='check']").each(function(i, y){//i为遍历的底标
var $num = $(y);//将DOM对象转为JQ对象的方法
alert($num.attr("name"))
})
============================================================================================================================================
//设置背景为灰,让背景后页面无法点击
#hidebg{position:absolute;left:0px;top:0px; background-color:#c0c0c0;width: 100%; filter:alpha(opacity=60);opacity:0.6;height: 100%;}
width:100%; /*宽度设置为100%,这样才能使隐藏背景层覆盖原页面*/
filter:alpha(opacity=60); /*设置透明度为60%*/
opacity:0.6; /*非IE浏览器下设置透明度为60%*/
============================================================================================================================================
//JQ中删除数组里的某个数据(splice两个参数,第一个是数组中的一个序号,第二个是要删除的元素个数)
var arr = ['a','b','c','d'];
arr.splice($.inArray('c',arr),1);
alert(arr);
============================================================================================================================================
//清空JS中的数组数据
var break_num = new Array();
break_num.splice(0, break_num.length);
============================================================================================================================================
//文件下载
<a href="jsp/download.jsp?path=<%=getServletContext().getRealPath("stu_score.xls") %>"></a>

<body>
<%
response.setCharacterEncoding("utf-8");
String pat = request.getParameter("path");
pat = new String(pat.getBytes("iso-8859-1"));
File file = new File(pat);
InputStream in = new FileInputStream(file);
OutputStream os = response.getOutputStream();
response.addHeader("Content-Disposition", "attachment;filename="+new String(file.getName().getBytes("gbk"),"iso-8859-1"));
response.addHeader("Content-Length", file.length()+"");
response.setContentType("application/octet-stream");
int data = 0;
while((data = in.read()) != -1){
os.write(data);
}
os.close();
in.close();

/* response.setCharacterEncoding("utf-8");
String pat = request.getParameter("path");
pat = new String(pat.getBytes("iso-8859-1"));
String fileName = pat.substring(pat.lastIndexOf("/"));
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setContentDisposition(null);
su.downloadFile(fileName); */
%>
</body>
============================================================================================================================================
//service的做法(类似于DAO层,用工厂进行实例,然后serverlet类里只在doget\dopost里像DAO那样调方法,调方法时将req\resp传参过去)
============================================================================================================================================
//$().each()与$.each()区别
$().each()在DOM对象上的处理比较多,比如下面的例子:
$("input").each(function(i,v){
if($(this).attr("checked") == true){
}
});
$.each()更多的用来遍历数组或对象:
//遍历数组:
var arr = [[1,2,3],[4,5,6],[7,8,9]];
$.each(arr, function(i,item){
console.log(item[0])//输出1 4 7
})
//遍历对象:
var obj = {one:1, two:2, three:3};
$.each(obj,function(key, value){
console.log(obj[key])//输出1 2 3
})
============================================================================================================================================
解决JSP页面上让Double数值正常显示的问题,不再显示科学计数:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> //导包
<fmt:formatNumber value="${OnePackage.data_price }" pattern="0.0000"/> //value就是要显示的数值 pattern是显示的格式
============================================================================================================================================
归属地:导area.js这个JS
<div>
<select id="s_province" name="s_province"></select>??
<select id="s_city" name="s_city" ></select>??
<script class="resources library" src="js/area.js" type="text/javascript"></script>
<script type="text/javascript">_init_area();</script>
</div>

<script>
var Gid = document.getElementById ;
var showArea = function(){
Gid('show').innerHTML = "<h3>省" + Gid('s_province').value + " - 市" +
Gid('s_city').value + " - 县/区" +
Gid('s_county').value + "</h3>"
}
Gid('s_county').setAttribute('onchange','showArea()');
</script>
============================================================================================================================================
返回上一页:
============================================================================================================================================
$(document).keydown(function(even){
console.log("even:"+even);
if(event.keyCode == '13'){
console.log('回车键!');
$('#login').click();//设置敲击此快捷键后ID为login的按钮触发点击事件
}
});
============================================================================================================================================

posted on 2017-07-06 21:21  猴子敲代码  阅读(196)  评论(0编辑  收藏  举报