学Jquery的时候感觉很熟悉,因为在项目中有很多都用到过,大同小异,甚至还有一种一点就通的感觉,因为以前不懂的现在就懂了
$(document).ready(function)({ //取出节点给变量 var userNameNode=$("#userName"); //点击给节点触发事件 $("userNamebutton").click(function(){ //.val 表示获得该节点的值 var username=userNameNode.val(); //判断该节点的值是否为空 if (userName==""){ alert("用户名不能为空"); }else{ //把地址传到后台 encodeURI:防止乱码,返回到了response $.get("url"+ encodeURI(encodeURI(userName)) , null, function(response){ //接受服务器端返回的数据,填充到div中 //给某个节点加html属性 $("#result").html(response); });//服务器地址 } }); //键盘弹起事件执行此方法 userNameNode.keyup(function(){ var value=userNameNode.val(); if(value==""){ //让边框变为红色,添加css属性 userNameNode.addclass("userText"); }else{ //去掉边框和红色,去掉css属性 userNameNode.removeClass("userText"); } }) })
.val()方法表示获得节点的value属性值
.html()方法可以设定某个节点中的html属性值
.click()可以相应鼠标点击事件
.keyup()方法可以相应键盘弹起的事件
$.get()方法可以和服务器端进行get方式的交互
addclass() removeclass() 方法可以给某个节点添加和删除一个class
把发送给服务器端的数据在JavaScript职工做两次encodeURL,然后在服务器端的代码中按UTF-8的方法做一次URLDecode,可以解决中文乱码问题