是一個(gè)前端框架

創(chuàng)新互聯(lián)公司是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站備案、服務(wù)器租用、空間域名、軟件開(kāi)發(fā)、微信小程序開(kāi)發(fā)等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運(yùn)營(yíng)推廣經(jīng)驗(yàn)的科技公司,有著多年的網(wǎng)站建站經(jīng)驗(yàn),致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開(kāi)一個(gè)面向全國(guó)乃至全球的業(yè)務(wù)窗口:建站服務(wù)熱線(xiàn):18980820575
jQuery是一個(gè)框架,在學(xué)jQuery之前可以先思考一個(gè)問(wèn)題:如何去理解框架?
其實(shí)框架的本質(zhì)從某種意義上來(lái)說(shuō)就是代碼的封裝。
對(duì)于使用者來(lái)說(shuō)只需要調(diào)用框架里的方法或?qū)傩詫?shí)現(xiàn)某種功能即可。
至于具體是如何實(shí)現(xiàn)的?使用者不用管,框架里面會(huì)實(shí)現(xiàn)。
所以一般情況下,框架編寫(xiě)的越復(fù)雜,其使用也就越簡(jiǎn)單。
jQuery對(duì)象是通過(guò)jQuery包裝DOM對(duì)象后產(chǎn)生的對(duì)象
注意:jQuery對(duì)象只能使用jQuery里的方法,DOM對(duì)象只能使用DOM對(duì)象的方法
基本選擇器
層級(jí)選擇器:
基本篩選器:
屬性選擇器:
表單篩選器:
篩選器方法:
jQuery的一些方法:
注意:對(duì)于標(biāo)簽上有的能夠看到的屬性和自定義屬性用attr()方法
對(duì)于返回布爾值比如checkbox,radion,option這三個(gè)標(biāo)簽是否被選中,用prop方法
事件綁定方式:
注意:DOM定義的事件可以用 .on()方法來(lái)綁定事件,但是jQuery定義的事件就不可以
常用事件有:
移除事件:把on改成off,就是移除 .on()綁定的事件
阻止后續(xù)事件執(zhí)行:事件函數(shù)中添加 return false; (常用于阻止表單提交等)或者e.preventDefault()
阻止事件冒泡:添加e.stopPropagation()
利用父標(biāo)簽去捕獲子標(biāo)簽的事件
推薦閱讀:
前端開(kāi)發(fā)框架之jQuery 和 Vue 的選擇
前端開(kāi)發(fā)之15個(gè)jQuery小技巧分享
前端開(kāi)發(fā)之JQuery入門(mén)基礎(chǔ)操作
前端開(kāi)發(fā)框架jQuery的優(yōu)勢(shì)與基礎(chǔ)知識(shí)分享
由于項(xiàng)目用的是SSH框架,又用到了Freemarker模板頁(yè)面,數(shù)據(jù)庫(kù)是Mysql5.0,主要內(nèi)容如下所示:
1. 注冊(cè)頁(yè)面,用Freemarker所寫(xiě),這里使用了JQuery的一個(gè)表單驗(yàn)證框架formvalidator:
對(duì)前臺(tái)注冊(cè)頁(yè)面register.ftl進(jìn)行表單驗(yàn)證,當(dāng)符合基本驗(yàn)證規(guī)則后,再通過(guò)JQuery formvalidator Ajax將數(shù)據(jù)傳到action中。
register.ftl:
html
head
meta http-equiv=Content-Type content=text/html; charset=utf-8 /
title會(huì)員注冊(cè)/title
link href=${base}/forum/css/pass.css rel=stylesheet type=text/css
link href=${base}/css/validator.css rel=stylesheet type=text/css/
script type=text/javascript src=@s.url value='/js/showError.js'//script
script type=text/javascript src=@s.url value='/js/jquery-1.3.2.min.js'//script
script type=text/javascript src=${base}/js/formValidator.js charset=UTF-8/script
script type=text/javascript src=${base}/js/formValidatorRegex.js charset=UTF-8/script
!—下面的JS代碼是,實(shí)時(shí)驗(yàn)證用戶(hù)名或注冊(cè)郵箱是否存在 --
script type=text/javascript
$(document).ready(function(){
$.formValidator.initConfig({formid:form1,onerror:function(msg){},onsuccess:function(){return true;}});
$(#username)
.formValidator({onshow:請(qǐng)輸入用戶(hù)名,onfocus:請(qǐng)您記住填寫(xiě)的用戶(hù)名,以后登錄時(shí)需要用到!,oncorrect:該用戶(hù)名可以注 冊(cè)})
.inputValidator({min:4,max:15,onerror:用戶(hù)名長(zhǎng)度不能小于4個(gè)字符})
.ajaxValidator({
type : get,
datatype: json,
data: username:+$(#username).val(),
url : ${base}/user/ajax/userCheck!checkUser.action,
success : function(data){if(data.userExist){return false;}else{return true;}},
buttons: $(#button),
error: function(){alert(服務(wù)器沒(méi)有返回?cái)?shù)據(jù),可能服務(wù)器忙,請(qǐng)重試);},
onerror : 該用戶(hù)名已經(jīng)存在,請(qǐng)更換用戶(hù)名,
onwait : 正在對(duì)用戶(hù)名進(jìn)行合法性校驗(yàn),請(qǐng)稍候…
});
$(#email)
.formValidator({onshow:請(qǐng)輸入郵箱,onfocus:郵箱6-100個(gè)字符,請(qǐng)正確輸入,oncorrect:恭喜你,郵箱可以注冊(cè),forcevalid:true})
.inputValidator({min:6,max:100,onerror:你輸入的郵箱長(zhǎng)度非法,請(qǐng)確認(rèn)})
.regexValidator({regexp:^([\\w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([\\w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$,onerror:你輸入的郵箱格式不正確})
.ajaxValidator({
type : get,
datatype: json,
data: email:+$(#email).val(),
url : ${base}/user/ajax/userCheck!checkUser.action,
success : function(data){if(data.emailExist){return false;}else{return true;}},
buttons: $(#button),
error: function(){alert(服務(wù)器沒(méi)有返回?cái)?shù)據(jù),可能服務(wù)器忙,請(qǐng)重試);},
onerror : 該郵箱已經(jīng)存在,請(qǐng)更換郵箱,
onwait : 正在對(duì)郵箱進(jìn)行校驗(yàn),請(qǐng)稍候…
});
});
/script
/head
body
div id=passmain
div class=passbottom
div class=pbleftb
h3 style=text-align:center;新用戶(hù)注冊(cè)/h3
p class=p2t帶*號(hào)的項(xiàng)目為必選項(xiàng), 請(qǐng)全部填寫(xiě)/p
ul class=p2ul
@s.form action=register method=POST theme=simple id=form1 name=form1
@s.token name=token/
li class=p2li
div class=p2left用戶(hù)登錄昵稱(chēng)span style=color:#FF0000;* /span/div
div class=p2right@s.textfield name=user.username id=username class=item_input size=30 maxlenght=20 required=true onblur=checkUser(); /
/div
div class=p2centerdiv id=usernameTip/
/div
/libr
li class=p2li
div class=p2left性別span style=color:#FF0000;* /span/div
div class=p2right@s.radio
name=user.gender
list={action.getText('user.gender.male'):action.getText(user.gender.male),action.getText('user.gender.female'):action.getText(user.gender.female)}
size=30//div
/libr
li class=p1t注意:郵箱必須是可以收到程序員之家激活碼的。/li
li class=p1t用戶(hù)注冊(cè)后,需要激活,才可以使用!激活碼只會(huì)發(fā)到您注冊(cè)郵箱中。/li
li class=p2li
div class=p2leftEmail地址span style=color:#FF0000;* /span/div
div class=p2right@s.textfield name=user.email id=email class=item_input size=30 onblur=checkEmali(); required=true/
/div
div class=p2centerdiv id=emailTip/div/div
/libr
li class=p1t5-20位字母、數(shù)字或字符組合,首字母必須為字母。/li
li class=p1t為了您的帳戶(hù)安全,強(qiáng)烈建議您的密碼使用字符+數(shù)字+特殊字符方式,并且密碼長(zhǎng)度大于8位。/li
li class=p2li
div class=p2left密碼span style=color:#FF0000;* /span/div
div class=p2right
@s.password name=user.password id=userPassword class=item_input size=30 maxlength=16 onblur=checkPassword(); required=true/
/div
div class=p2centerdiv id=divHintPassword style=display:none;/div
/div
/libr
li class=p2li
div class=p2left重復(fù)輸入密碼span style=color:#FF0000;* /span/div
div class=p2right@s.password name=verifyPassword id=verifyPassword class=item_input size=30 maxlength=16 onblur=validatePassword(); required=true//div
div class=p2center
div id=divHintverifyPassword style=display:none;/div
/div
/libr
br
li class=p2li
div class=p2left輸入校驗(yàn)碼span style=color:#FF0000;*/span /div
div class=p2right@s.textfield name=validationCode class=item_input size=10 required=true/
/div
/li
li class=p2li
div class=p2left校驗(yàn)碼span style=color:#FF0000;*/span /div
div class=p2rightimg src=@s.url value='/vcode.jpg'/ align='absmiddle' border='0'//div
/li
li class=p1tinput name=提交 type=submit value=注冊(cè)用戶(hù) /
/li
/ul
/div
/@s.form
/div
/div
/body
/html
2.后臺(tái)Action類(lèi),UserAjaxAction.java:
基于Hibernate與數(shù)據(jù)庫(kù)進(jìn)行交互驗(yàn)證用戶(hù)名和郵箱是否存在,同時(shí)返回的數(shù)據(jù)格式為JSon類(lèi)型的。
package com.qiuyanluocheng.action;
import IO.IOException;
import com.googlecode.jsonplugin.annotations.JSON;
/**
*
* @author qiuyanluocheng
*
*/
public class UserAjaxAction extends BaseAction
{
/**
* BaseAction是自己封裝的
*/
private static final long serialVersionUID = 1L;
private String username;// 用戶(hù)名
private String email;// 郵箱
private boolean userExist;
private boolean emailExist;
// 表示返回的Json數(shù)據(jù)中不含此屬性
@JSON(serialize=false)
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public boolean isUserExist()
{
return userExist;
}
public void setUserExist(boolean userExist)
{
this.userExist = userExist;
}
public boolean isEmailExist()
{
return emailExist;
}
public void setEmailExist(boolean emailExist)
{
this.emailExist = emailExist;
}
// 檢查用戶(hù)是否存在
public String checkUser() throws IOException
{
if(username != null){
if(this.getUserService().isUsernameExist(username)){
// System.out.println(用戶(hù)存在!!!!!+username);
userExist = true;
}else{
// System.out.println(用戶(hù)不存在!!!!!+username);
userExist = false;
}
}
if(email != null){
if(this.getUserService().isEmailExist(email)){
// System.out.println(郵箱存在!!!!!+email);
emailExist = true;
}else{
// System.out.println(郵箱不存在!!!!!+email);
emailExist = false;
}
}
return ajax;
}
}
注意需要導(dǎo)入jsonplugin-0.34.jar包。
3. 配置文件struts.xml:
配置action,并且注意package要繼承json-default包,且結(jié)果集類(lèi)型為json。
?xml version=1.0 encoding=utf-8?
!DOCTYPE struts PUBLIC -//Apache Software Foundation//DTD Struts Configuration 2.0//EN dtds/struts-2.0.dtd
struts
package name=userAjax extends=json-default namespace=/user/ajax
action name=userCheck class=com.qiuyanluocheng.action.UserAjaxAction
!—返回結(jié)果類(lèi)型必須為json --
result name=ajax type=json/result
/action
/package
/struts
jQuery框架中增加對(duì)輸入框的驗(yàn)證,在設(shè)計(jì)頁(yè)面表單時(shí),常常因?yàn)樾枰獙?xiě)大量的驗(yàn)證信息,還要寫(xiě)復(fù)雜的正則表達(dá)式,現(xiàn)在這樣一個(gè)驗(yàn)證框架可以解決這樣的繁雜問(wèn)題。這樣jquery.validate.js就誕生了,jquery.validate.js是jquery旗下的一個(gè)驗(yàn)證框架,借助jquery的優(yōu)勢(shì),我們可以迅速驗(yàn)證一些常見(jiàn)的輸入,并且可以自己擴(kuò)充自己的驗(yàn)證方法,并且對(duì)國(guó)際化也有很好的支持 。
validate自帶的默認(rèn)驗(yàn)證
required: 必選字段,
remote: 請(qǐng)修正該字段,
email: 請(qǐng)輸入正確格式的電子郵件,
url: 請(qǐng)輸入合法的網(wǎng)址,
date: 請(qǐng)輸入合法的日期,
dateISO: 請(qǐng)輸入合法的日期 (ISO).,
number: 請(qǐng)輸入合法的數(shù)字,
digits: 只能輸入整數(shù),
creditcard: 請(qǐng)輸入合法的信用卡號(hào),
equalTo: 請(qǐng)?jiān)俅屋斎胂嗤闹?
accept: 請(qǐng)輸入擁有合法后綴名的字符串,
maxlength: jQuery.format(請(qǐng)輸入一個(gè)長(zhǎng)度最多是 {0} 的字符串),
minlength: jQuery.format(請(qǐng)輸入一個(gè)長(zhǎng)度最少是 {0} 的字符串),
rangelength: jQuery.format(請(qǐng)輸入一個(gè)長(zhǎng)度介于 {0} 和 {1} 之間的字符串),
range: jQuery.format(請(qǐng)輸入一個(gè)介于 {0} 和 {1} 之間的值),
max: jQuery.format(請(qǐng)輸入一個(gè)最大為 {0} 的值),
min: jQuery.format(請(qǐng)輸入一個(gè)最小為 {0} 的值)
分享文章:jquery框架,jquery框架封裝
網(wǎng)址分享:http://chinadenli.net/article30/dsgojpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、小程序開(kāi)發(fā)、靜態(tài)網(wǎng)站、微信公眾號(hào)、定制開(kāi)發(fā)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)