外包做网站平台 一分钟,推广策划方案怎么做,微信管理标签,国内网站推广一 JavaScript 的书写形式
1.1 行内式
input typebutton value点我一下 onclickalert(hello akai); 注意,JS 中的字符串常量可以用单引号表示,也可以使用双引号表示.HTML 中推荐使用双引号,JS 中推荐使用单引号(使用双引号容易…一 JavaScript 的书写形式
1.1 行内式
input typebutton value点我一下 onclickalert(hello akai); 注意,JS 中的字符串常量可以用单引号表示,也可以使用双引号表示.HTML 中推荐使用双引号,JS 中推荐使用单引号(使用双引号容易出现截断问题) 1.2 内嵌式 写到 script 标签中(建议写在/body之前) scriptalert(hello akai);/script script 标签最好写在 /body 前,需要根据场景需求放置标签位置,因为HTML 中的代码是根据顺序执行的,可能会出现变量为初始化的场景. 例: headscriptconsole.log(document.getElementById(but).val);/script
/head
bodyinput idbut typebutton value点我一下 onclickalert(hello akai);
/body 1.3 外部式
写到单独的 .js 文件中(建议写在/head 之前)
bodyscript srctool.js/script
/body 注意, 这种情况下 script 中标签中间不能写代码, 必须空着(写了代码也不会执行) 二 输入输出
2.1 输入: prompt
弹出一个输入框
// 弹出一个输入框
scriptprompt(请输入你的名字:);
/script
可以使用变量来接收 prompt 返回值.
2.2 输出: alert
// 弹出一个输出框
scriptalert(hello);
/script
2.3 输出: console.log
// 在控制台打印
scriptconsole.log(这是一条测试);
/scipt
三 比较运算符 比较相等 , 会进行隐试的转换 比较相等, 不会进行隐试类型的转换
四 数组
4.1 创建数组 script// 使用new 关键字创建var arrnew Array();// 使用字面常量方式创建var brr[]var crr[1,2,haha,true];crrhello;console.log(crr);console(crr[1])//此时控制台上是不会有打印结果的/script
js 中数组的元素下标可以不连续
注意:不要给数组名直接赋值,此时数组中的所有元素都没有了
4.2 新增数组元素
相当于在末尾新增元素,新增的元素默认值为 undefined scriptvar arr[9,3,4,5];arr.length5;console.log(arr);console.log(arr[4]);//控制台打印undefined/script
通过下标新增 scriptvar arr[9,3,4,5];arr[5]5;console.log(arr[4]);/script
使用 push 进行追加元素 scriptvar arr[1,2,3,4,5,6];var newArr[];for(var i0;iarr.length;i){if(arr[i]%21)newArr.push(arr[i]);}/script
4.3 删除数组中的元素 scriptvar arr[1,2,3,4,5,6];//从下标为2 的元素开始,删除2个元素arr.splice(2,2);//slice 不会改变原数组的结构,获取下标2和3 的元素var loarr.slice(2,4);console.log(arr);/script
五 函数
5.1 语法格式 function 函数名(形参列表){ 函数体 return 返回值 } //函数调用 函数名 //不考虑返回值 返回值函数名(实参列表) //考虑返回值 • 函数定义并不会执行函数体内容,必须要调用才会执行,调用几次就会执行几次 scripthello();hello();hello();function hello(){console.log(hello);}/script
函数的定义和调用的先后顺序没有要求,且创建函数时形参可以不用声明类型
关于参数的个数
1)如果实参的个数比形参的个数多,则多出的参数不参与函数的运算
2)如果室参的个数比形参的个数少,则此时多出来的形参的是为 undefined
5.2 函数表达式 scriptvar addfunction(){var sum0;for(var i0;iarguments.length;i){sumarguments[i];}return sum;}console.log(add(10,20));//30console.log(add(1,2,3,4));//10console.log(typeof add);//function/script
此时形如 function(){} 这样的写法定义了一个匿名函数,然后将这个匿名函数用一个变量来表示,后面就可以通过这个 add 的变量来调用函数了
六 对象的使用
使用{} 创建对象 scriptvar teacher{};//创建一个空对象var student{name: akai,height: 170,weight: 120,sayHello: function(){console.log(hello);}}student.sayHello();/script • 可以直接使用 { } 创建一个空对象 • 属性和方法使用键值对的形式来组织(类似于HashMap) • 键值对之间使用 , 分割. 最后一个属性后面的,可有可无 • 方法的值是一个匿名函数 使用对象的属性和方法:
script //使用 .成员 访问运算符来访问属性 console.log(student.name);// 使用 [ ] 访问属性,此时属性需要加上引号console.log(student[height]);// 调用方法,别忘记加(),student.sayHello();
/script
七. 猜数字
h3猜数字游戏/h3
请输入要1-10的数字:
input typetext idinputNump/p
input typebutton value查看结果 onclickcheckRight()br/
div idprint/divscriptfunction checkRight(){var randNumMath.floor(Math.random()*101);var inputNumdocument.getElementById(inputNum).value;var mes;if(inputNumrandNum){mesh4恭喜你猜对了/h4;}else{mesh4你猜错了,正确答案是:randNum/h4;}document.getElementById(print).innerHTMLmes;}/script八 表白墙
bodydiv stylewidth: 100%;text-align: center;h3表白墙/h3谁nbsp;nbsp;nbsp;nbsp;nbsp;:input idfromp/p对谁nbsp;nbsp;:input idtop/p说 nbsp;nbsp;nbsp;nbsp;:input idmesp/pinput typebutton value提交 onclickcheck()div idwall/div/divscriptfunction check(){var fromM$(#from);var toM$(#to);var mesM$(#mes);if(fromM.val().trim()){alert(请先输入你的名字);fromM.focus();return;}if(toM.val().trim()){alert(请先输入对方的名字);toM.focus();return;}if(mesM.val().trim()){alert(请先输入要发送的信息);mesM.focus();return;}$(#wall).append(fromM.val()对toM.val()说mesM.val()p/p);// 将表格数据清空fromM.val();toM.val();mesM.val();}/script/body 文章转载自: http://www.morning.gnmhy.cn.gov.cn.gnmhy.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.jjzbx.cn.gov.cn.jjzbx.cn http://www.morning.ryxdr.cn.gov.cn.ryxdr.cn http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn http://www.morning.jtmrx.cn.gov.cn.jtmrx.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.nxhjg.cn.gov.cn.nxhjg.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.dwwlg.cn.gov.cn.dwwlg.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.pluimers.cn.gov.cn.pluimers.cn http://www.morning.pqxjq.cn.gov.cn.pqxjq.cn http://www.morning.gqddl.cn.gov.cn.gqddl.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.rnds.cn.gov.cn.rnds.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.klzdy.cn.gov.cn.klzdy.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.tlnkz.cn.gov.cn.tlnkz.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.dbnpz.cn.gov.cn.dbnpz.cn http://www.morning.qmrsf.cn.gov.cn.qmrsf.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn