PC端一些基本常识总结

60.为某个标签添加属性
$(function(){
    var phone = '${phone}';
    if (phone != null) {
        $("#remember").prop("checked",true);
    }
});
$( "#staff_info" ).css({"width":50px,"height":30px});
这两者的区别是宽跟高是style里面的,checked不是,是等同于style一个级别的。
59.获取复选框的值
$("input[type='checkbox']").attr('value')
返回结果:501
$("input[type='checkbox']").is(':checked')
返回结果:选中=true,未选中=false
58.session.setMaxInactiveInterval(3600);//3600秒,注意服务器端的3600秒,而不是客户端的
57.自定义request和response
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
56.在jsp页面的JavaScript代码块中填写代码,即是用js获取request等域中的值,代码如下:
function downLoad(){
        var comid = ${comid};
        var usersid = "";
        <c:forEach items="${list }" var="l" begin="0" end="0">
            usersid = ${l.usersid};
        </c:forEach>
            /* $.post("",{},function(data){}); */
        var url="${pageContext.request.contextPath}/location/downUserInfo.action?usersid="+usersid+"&comid="+comid;
     location.href = url;
    }
55.用txt编辑静态页面时,记得另存为时修改编码格式,改为utf-8,以及抬头这两段代码。如下:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <input style="width: 100px;height:25px;" type="text"/>
    <input type="button" value="提交"/>
</body>
</html>
54.关于流必须要记住的两段代码:
BufferedReader bufr = new BufferedReader(new OutputStreamReader(System.in));

BufferedWriter bufw = new BufferWriter(new InputStreamWriter(System.out));
53.checkbox的一些全选与全不选以及反选。
<form>
        你爱好的运动是?<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="CheckAll" onclick="all();" value="全选" />
    <input type="button" id="CheckNo" value="全不选" />
    <input type="button" id="CheckRev" value="反选" />
    </form>
  </body>
  <script type="text/javascript">
    $(function(){
        $("#CheckAll").click(function(){
            $("input:checkbox").prop("checked","checked");
        });
        $("#CheckNo").click(function(){
            $("input:checkbox").removeAttr("checked");
        });
        $("#CheckRev").click(function(){
            $("input:checkbox").each(function(){
                this.checked=!this.checked;
            });
        });
    });
</script>
52.<a onclick="if (confirm('确定要退出吗?')) return true; else return false;"href="${pageContext.request.contextPath}/backmanage/loginout.action">退出</a></li>
一个简单的退出 ,loginout做清空session操作。
51.字符匹配汉字
if(!name.matches("^[\u4e00-\u9fa5]+$")){
}
50.el表达式 ${data} 相当于 pageContext.findAttribute("data");会在page request session application这四个域中 逐个去找data
当el表达式用 . 号取不出复杂类型数据时,用 [] ;

49.jsp注释(<%-- --%>)与html注释(<!-- -- >)两者的区别。
   本质结果在浏览器上查看源码,jsp注释的不会在浏览器上显示,但是html注释的会在浏览器上显示。
48.页面点击简单的弹出框,类似于alert但是可以为确定添加点击事件,代码如下:
    <a onclick="if (confirm('确定要退出吗?')) return true; else return false;" 
      href="${pageContext.request.contextPath}/backmanage/loginout.action">退出</a>
47.request.getAttribute("id");是取request域里面存的数据。
   request.getParameter("id");是取客户端带过来的数据。
46.serialize() 方法通过序列化表单值,创建 URL 编码文本字符串。
例如:
<form id="info">
First name: <input type="text" name="FirstName" value="Bill" /><br />
Last name: <input type="text" name="LastName" value="Gates" /><br />
</form>
<button id="test">序列化表单值</button>
$("#test").click(function(){
 alert($("#info").serialize());    
});
打印结果如下:FirstName=Bill&LastName=Gates
45.request.getAttribute("name");这是获取request域里面存的值。
   request.getParameter("name");这是获取客户端提交过来的数据。
44.解决request的get与post乱码,代码如下:
get乱码:String name = request.getParameter("name");
name = new String(name.getBytes("iso8859-1"),"utf-8");
post乱码:
request.setCharacterEncoding("utf-8");
//因为这端代码只对post请求有用。
String name = request.getParameter("name");
43.随机生成12位英文字母,代码如下:
public static String createCode(){
        for (int i = 0; i < 12; i++) {
            code += codes[(int) Math.floor(Math.random()*26)];
        }
        if (code.length() < 12) {
            createCode();
        }
        return code;

    }
42.response实现重定向:
response.setStatus(302);
response.setHeadler("location","day/index.jsp");
//这两句代码,可直接用如下代码代替:
response.sendRedirect("day/index.jsp");
重定向的特点:
1.浏览器会向服务器发送两次请求,会产生两对request跟response;
2.浏览器地址栏会发生变化。
41.控制浏览器缓存,代码如下:
response.setDateHeader("expires",System.currentTimeMillis()+1000*3600);
//注意时间的设置,一定是当前的时间去加上缓存的时间。缓存的截止时间可在浏览器internet选项有个设置,点开,查看文件中找到。
String data = "asdf";
response.getWrite.write(data);
1.onblur事件:会在对象失去焦点时发生。onBlur="if(this.value==''){this.value='请输入关键字...'}else{this.value=''}"
2.onload事件:页面加载之后执行的一段JavaScript。
3.load事件:该方法通过Ajax请求从服务器加载数据,并把返回的数据放到指定的元素中。
4.var charIndex = Math.floor(Math.random()*32);所代表的意思。
    Math.random()---代表随机会产生一个[0,1)的数。
    Math.random()*32---代表会产生一个[0,32)的数,注意其中包含了小数.
    Math.floor(Math.random()*32)会对由上面的语句产生的数值进行向下取整
    例如产生的数为5.5,则math.floor(5.5)=5,最终把5赋值给charIndex。
5. $("#codeMsg").text("验证码输入错误!");
    ---代表将“验证码输入错误!”这段文字放到id=“codeMsg”这个标签后面显示。
6.  $(document).ready(function(){ 
    createCode(); 
    });
        --这个方法与onload事件对应,onload是在页面加载完成后去执行,这个方法是在                页面加载前去执行。
7.页面拿值的两种方法。
    a.var name = $("#bname").val();
    b.var name = document.getElementById("name ");
8.${id} = request.getAttribute("id"); 这是el表达式
  $("#xxx").val;  这是jquery取值。
 9.onfocus事件:在对象获得焦点的时候发生。与onblur事件相对应。
 onFocus="if(this.value==''){this.value='请输入关键字...'}else{this.value=''}"
 10.为登录按钮添加登录事件。
     代码如下:$(function(){
                $(document).keydown(function(event){
                    if(event.keyCode == 13){
                    checkSubmit();//点击登录所执行的方法
                    }
                });
            })
11.jquery的each函数。
    代码如下:$("button").click(function(){
      $("li").each(function(){
        alert($(this).text())
      });
    });---输出每个 li 元素的文本
12.重定向(direct)与跳转(forward)区别,直接看地址栏,跳转地址栏不发生变化。
13.页面路径多参数传值写法:
--window.location.href="<%=request.getContextPath()%>/location/getUsersPower.action?id="+id+"&comid="+comid;
14.获取复选框的值:
        $(":checked").each(function(i){
             alert($(this).val());
        });
15.@Param标签的用法:@RequestParam(required = false, defaultValue = "1")
16.ajax请求的两种方式
    a.
    $.ajax( {  
                type : "POST",  
                async : false,
                url : "<%=request.getContextPath()%>/location/randomPage.action",
                data : {
                  'aa' : '${usersid}',//一定要注意这两个引号
                  'comid':'${comid}',
                  'num' : data,
                  'role' : '${roleId}'
                 }, 
               dataType:"json",  
              success: function(data) {

              }  
    b.
    $.post("url",{参数},function(){回调函数});
17.<td onclick='checkUserInfo(\""+data[int].phone+"\",\""+data[int].name+"\","+data[int].month+","+data[int].day+",\""+data[int].address+"\",\""+data[int].describe+"\",\""+data[int].progress+"\",\""+data[int].picture+"\","+data[int].usersid+");'><a style='color:#f00;text-decoration:none;' href='#'>查看详情</a></td>
    这里主要解释转义字符“\”,当有一个单引号,如果再先单引号,直接计算机解释为单引号结束,因此需要添加转义字符。转义字符就是将后面的转为双引号。
18.页面中当报length方法又错误,就是没有判断非空,js判断非空有三种情况,代码如下:
    if (role == null || role== undefined || role == "") {
            role = "no";
        }
19.<inout>标签,默认字体为灰色,当输入之后变为黑色,代码如下:
        <input type="text" name="address" size="60" maxlength="60" style="color:gray" value="(测试)" onfocus="if(this.value=='(测试)'){this.value=''};this.style.color='black';" onblur="if(this.value==''||this.value=='(测试)'){this.value='(测试)';this.style.color='gray';}">
20.主要说明两个js接受数据的区别,一个是ajax请求数据返回的data数据,另一个就是jquery的$("#id").load("url",canshu,function(data){}) 主要是这两个data的不同情况,假如返回的data为两个list的json串,那么ajax返回的 data.length其值为2,而jquery返回的 data.length 其值应该为该json串的整体长度 例如156.所以当jquery的回调函数去处理data的时候,需要用到eval()函数,例如: var result = eval(data);这样result.length 的值就变成了2.
21.返回上一个页面的方法。前提是地址栏没有任何变化。
    location.reload(); 
22.jquery动态加载下拉框数据。
代码如下:<select name="department" id="department" style="width:130px;" onclick="searchDepartment();">
js代码:function searchDepartment(){
        $.ajax( {  
                type : "POST",  
                async : false,
                url : "<%=request.getContextPath()%>/location/queryDepartment.action",
               dataType:"json",  
              success: function(data) {
                var result = "";
                if (data.length > 0) {

                    for (var i = 0; i < data.length; i++) {
                        result +="<option value='"+data[i].id+"';>"+data[i].departmentName+"</option>";
                    }
                }
                $("#department").append(result); 
              }           
            });
     }
  获取下拉框动态值的办法就是 $("#selectid").val();
  23.页面展示图片:不要忘记这个“=”号。
  <img style="width:150px;height:100px;" src="<%= basePath%>img/linqingxia.png"/>
  24.图片上传:MultipartHttpServletRequest与HttpServletRequest的两种方法,这里只说HttpServletRequest的方法,public void yanzhengzz(HttpServletRequest request, String zhizhaonums, String dianming,
            HttpSession session) {
        loginqian user1 = (loginqian) session.getAttribute("loginqian");
        loginqian user = loginqianmapper.selectByPrimaryKey(user1.getId());
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile imgFile = multipartRequest.getFile("imgFilezhizhao");
        MultipartFile imgFile2 = multipartRequest.getFile("legalcard");
        try {
            if (user.getZhizhaoyanzheng() == null) {
                // 奖励
                Add add = new Add();
                add.add_isMe("营业执照认证", user.getId());
            }
            if (imgFile != null) {
                String identitypic = upload.add(request, imgFile, "pic",
                        "yingyezhizhaopic");
                if (identitypic != null) {
                    user.setZhizhaopic(identitypic);
                    user.setZhizhaoyanzheng(1);
                    String time = new SimpleDateFormat("yyyy-MM-dd")
                            .format(new Date());
                    user.setZhizhaoyztime(time);// 验证时间
                    user.setZhizhaoshenhe(0);
                    user.setZhizhaonums(zhizhaonums);
                }
            }
            if (imgFile2 != null) {
                String legalcard = upload.add(request, imgFile2, "pic",
                        "yingyezhizhaopic");
                if (legalcard != null) {
                    user.setLegalcard(legalcard);
                }
            }
            loginqianmapper.updateByPrimaryKey(user);
            Dianmian dm=dianmianMapper.findbysid(user1.getId());
            dm.setDianming(dianming);
            dianmianMapper.updateByPrimaryKeySelective(dm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 25.<a>标签如何取消href跳转:<a href='javascript:void(0);'></a>
 26.$("#box_both").load("${pageContext.request.contextPath}/location/fuzzySearchStaff.action","province":province,"city":city,"county":county,"account":account,"name":name,"state":state,"comid":comid},function(data){});
 27.input输入框中的鼠标点击与离开,代码如下:onFocus="if(this.value==''){this.value=='请输入账号'}else{this.value=''}" onBlur="if(this.value==''){this.value='请输入账号'}"
 28.页面跳转路径的几种情况:
 第一种:$.ajax( {  
                type : "POST",  
                async : false,
                url : "<%=request.getContextPath()%>/location/queryPosition.action",
               dataType:"json",  
              success: function(data) {

              }           
            });
第二种:location.href = "${pageContext.request.contextPath}/location/turnPage.action?name="+name;

第三种: <script  src="<%= basePath%>js/pagination.js"></script>
29.弹框的位置设定:#picture{
        position: absolute;
        top:300px;
        left:300px;
    }
30.一块div放一个图片,然后在图片上拼接相应的标签,有两种方法:
    第一种:<div id="picture">
                <img src="<%=basePath%>img/warn.png" width="450" height="200">
            </div>
然后为div设置相对位置,#picture{
        position: absolute;
        top:300px;
        left:300px;
    }
然后在添加各个标签,依旧需要设置相对位置,比较繁琐。
第二种:直接将图片设为该div的背景图,<div id="picture">
            </div>
然后设置样式即可,代码如下:#picture{
    position: absolute;
    top:300px;
    left:40%;
    background:url(img/linqingxia.png) no-repeat center;
    width: 450px;
    height: 200px;
}
31.页面加载数据,显示下拉条的属性,只需要给当前区域添加css样式 overflow:scroll;
32.jquery在js中为某些标签添加样式代码如下:
    $( "#staff_info" ).css( "backgroundColor","rgba(0,0,0,.3)" );或者
    $( "#staff_info" ).css({"width":50px,"height":30px});
33.div的隐藏,一个是在标签内部加样式,一个是在css里面加样式,代码如下:
<div id="picture" hidden="hidden">
#picture{
    display:none;
}
34.A实体类集成B实体类,查询结果返回B实体类,将其属性值拷贝到A实体类中,代码如下:
BeanUtil.copyProperties(B,A);
35.页面乱码,一般注意检查jsp是否添加如下代码:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>或者是头部是否添加如下代码:<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />或者<meta charset=utf-8 />
36.el表达式从request域拿值,如果是汉字需要添加单引号。
'${name}' = request.getAttribute("name");
37.response.setHeader("Content-type","text/html;charset=UTF-8");
//设置响应头部,通知浏览器以何种形式打开。
String data = "中国";
OutputStream out = response.getOutputStream();
out.write(data.getBytes("UTF-8"));
38.response向浏览器输出
response.setCharacterEncoding("UTF-8");
//服务器将数据给response响应,response是老外写的,所以回用iso8859-1去传到浏览器,所以需要去给response的响应,指定码表。
response.setHeader("content-type","text/html;charset=UTF-8");
//这段代码可以去代替前面两段代码
response.setContentType("text/html;charset=UTF-8");
String data = "中国";
//这个主要是告诉浏览器,response的响应为那个码表。
PrintWrite out = response.getWriter();
out.write(data);
39.input输入框,只能输入数字:
<input type='text' onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();" />
40.设置浏览器间隔多久跳转路径,代码如下:
response.setHeader("refresh","3;url=''");
如果为jsp页面设置间隔刷新,使用如下代码:
<meta http-equiv='refresh' content='3;url=/day06/test.jsp'>

猜你喜欢

转载自blog.csdn.net/past__time/article/details/78124634