20条件判断
1:
2:
--
3:
varuserChoice=window.confirm(“ChooseOKorCancel”);
4:
varresult=(userChoice==true)?
“OK”:
“Cancel”;
5:
document.write(result);
6:
//-->
7:
21指定次数循环
1:
2:
--
3:
varmyArray=newArray(3);
4:
myArray[0]=“Item0”;
5:
myArray[1]=“Item1”;
6:
myArray[2]=“Item2”;
7:
for(i=0;i 8: document.write(myArray 2: 浏览器输出 26访问document对象 1: 2: varmyURL=document.URL; 3: window.alert(myURL); 4: 27动态输出HTML 1: 2: document.write(“ Here’ssomeinformationaboutthisdocument:
3:
document.write(“
4:
document.write(“
“+document.referrer+“
5:
document.write(“
“+document.domain+“
6:
document.write(“
“+document.URL+“
7:
document.write(“
”);
8:
28输出换行
1:
document.writeln(“a”);
2:
document.writeln(“b”);
29输出日期
1:
2:
varthisDate=newDate();
3:
document.write(thisDate.toString());
4:
30指定日期的时区
1:
2:
varmyOffset=-2;
3:
varcurrentDate=newDate();
4:
varuserOffset=currentDate.getTimezoneOffset()/60;
5:
vartimeZoneDifference=userOffset-myOffset;
6:
currentDate.setHours(currentDate.getHours()+timeZoneDifference);
7:
document.write(“ThetimeanddateinCentralEuropeis:
“+currentDate.toLocaleString());
8:
31设置日期输出格式
1:
2:
varthisDate=newDate();
3:
varthisTimeString=thisDate.getHours()+“:
”+thisDate.getMinutes();
4:
varthisDateString=thisDate.getFullYear()+“/”+thisDate.getMonth()+“/”+thisDate.getDate();
5:
document.write(thisTimeString+“on“+thisDateString);
6:
32读取URL参数
1:
2:
varurlParts=document.URL.split(“?
”);
3:
varparameterParts=urlParts[1].split(“&”);
4:
for(i=0;i 5: varpairParts=parameterParts.split(“=”); 6: varpairName=pairParts[0]; 7: varpairValue=pairParts[1]; 8: document.write(pairName+“: “+pairValue); 9: } 10: 你还以为HTML是无状态的么? 33打开一个新的document对象 1: 2: functionnewDocument(){ 3: document.open(); 4: document.write(“ ThisisaNewDocument.
5:
document.close();
6:
}
7:
3:
页面跳转
1:
2:
window.location=“
4:
表单
还是先继续写完JS就这么回事系列吧~
43表单构成
1:
2:
3:
4:
5:
6:
7:
8:
9:
44访问表单中的文本框内容
1:
2:
3:
4:
45动态复制文本框内容
1:
2:
EntersomeText:
3:
CopyText:
4:
5:
6: document.myForm.myText.value;”>CopyTextField 46侦测文本框的变化 1: 2: EntersomeText: 3: 47访问选中的Select 1: 2: 3: 4: 5: 6: 7: 8: 48动态增加Select项 1: 2: 3: 4: 5: 6: 7: 8: document.myForm.mySelect.length++; 9: document.myForm.mySelect.options[document.myForm.mySelect.length-1].text=“3”; 10: document.myForm.mySelect.options[document.myForm.mySelect.length-1].value=“ThirdChoice”; 11: 49验证表单字段 1: 2: functioncheckField(field){ 3: if(field.value==“”){ 4: window.alert(“Youmustenteravalueinthefield”); 5: field.focus(); 6: } 7: } 8: 9: 10: TextField: 11: 12: 50验证Select项 1: functioncheckList(selection){ 2: if(selection.length==0){ 3: window.alert(“Youmustmakeaselectionfromthelist.”); 4: returnfalse; 5: } 6: returntrue; 7: } 51动态改变表单的action 1: 2: Username: 3: Password: 4: 5: 6: 7: 52使用图像按钮 1: 2: Username: 3: Password: 4: 5: 6: 53表单数据的加密 1: 2: -- 3: functionencrypt(item){ 4: varnewItem=''; 5: for(i=0;i 6: newItem+=item.charCodeAt(i)+'.'; 7: } 8: returnnewItem; 9: } 10: functionencryptForm(myForm){ 11: for(i=0;i 12: myForm.elements 70框架代码库 根据以上的一些思路,我们可以使用一个隐藏的框架页面来作为整个框架集的代码库 1: 2: 3: 4: 5: .value=encrypt(myForm.elements.value); 13: } 14: } 15: 16: //--> 17 copyright@ 2008-2022 冰豆网网站版权所有 经营许可证编号:鄂ICP备2022015515号-1