基础语法
变量
警示框
文档输出
控制台打印输出
输入语句
数组Array
let arr(数组名)=[1,2,3,4,5,6,7,8,9,10]
arr.push()
arr.unshift()
arr.pop
arr.shift
arr.splice(0,10)
|
模板字符串
检测数据类型
let c=99 consle.log(typeof c)
|
类型转换
let number= + prompt('输入字符串转为数字')
number.Number() number.parse()
number.toString()
|
保留整数
let c='123abc' parseInt(c) ----->123
|
保留小数及整数
let c='123.123abc' parseFloat(c) ------>123.123
|
函数
function 函数名(){ 函数体 return 数值 }
function chen(x,y){ return x+y }
|
匿名函数
let 名称=function(){ }
let chen=function(c,p){ return c+p }
|
立即函数
(fuction(){ })()
(function(x,y){ return x+y })(1,2)
|
对象
let 对象名 ={ } let 对象名 = new Object()
let chen={ name:'peng', age:18, where:"广东" }
console.log(chen.name) console.log(chen['name'])
chen.how='这是第一次出现则为新增,否则为修改'
delete chen.where
for(let i in chen){ console.log(chen[i]); }
console.log(Object.keys(chen))
let obj ={ name:'chen', song:function(x,y){ } }
|
Web-apis
①选择器
获取第一个匹配的元素
document.querySelector('css选择器') document.querySelector('ul(上层) li(本层)') const box=document.querySelector('div')
|
通过ID名选择
let idName=document.getElementById('id_name') idName.style.color='green' idName.style.fontSize='100px'
|
通过Class名选择
var className=document.getElementsByClassName("class_name")[0] className.style.fontSize='100px'
|
获取所有匹配的元素
const nav=document.querySelectorAll('.nav li')
|
document.getElementById('hhc')
document.getElementsByTagName('div')
document.getElementsByClassName('class')
|
修改属性
let image=document.querySelector('img') image.src='../../html/images/2.jpg'
|
修改样式
变量.style.backgroundColor='blue'
|
classlist类名操作
元素.classList.add('第二个类名')
元素.classList.remove('类名')
|
axios
发送数据
无参:axios.get/post(url).then(成功).catch(错误).finally()
有参:axios.get(url,,{params:{}}).then(成功).catch(错误).finally()
then:一个函数,表示请求成功时执行的
resp 是形参,表示服务器返回的数据,这个数据是由axios封装好的
<script> function funGet(){ let url = "http://localhost:9000/user/query" axios.get(url).then( resp =>{ console.log("请求成功,返回服务器的数据:"+resp); }).catch(err=>{ console.log("请求错误"); }).finally(()=>{ }) } function Get(){ let url = "http://localhost:9000/user/get?id=1002&name=彭呀"; axios.get(url).then( resp =>{ console.log("请求成功,返回服务器的数据:"+resp); }).catch(err=>{ console.log("请求错误"); }) } function Get2(){ let url = "http://localhost:9000/user/get"; axios.get(url,{ params:{ id:3000, name:'chen同学' } }).then(resp =>{ console.log("请求成功,返回服务器的数据:"+resp); }) .catch(err=>{ console.log("请求错误"); }) } function Post(){ let url = "http://localhost:9000/user/add"; axios.Post(url,"id=12345&name=彭呀") .then(resp =>{ console.log("请求成功,返回服务器的数据:"+resp); }) .catch(err=>{ console.log("请求错误"); }) } function Post2(){ let url = "http://localhost:9000/user/json"; let data={ id:999, name:"张三", sex:"女", age:20 } axios.post(url,data) .then(resp =>{ console.log("请求成功,返回服务器的数据:"+resp); }) .catch(err=>{ console.log("请求错误"); }) } </script>
|
function Post3(){ let url = "http://localhost:9000/user/json"; let param={ id:999, name:"张辰", sex:"女", age:20 } axios({ url:url, method:"post", data:param }).then(resp=>{ console.log("请求成功,返回服务器的数据:"+resp); }) }
|
接收数据
响应结构是一个json
{ // `data` 由服务器提供的响应 data: {},
// `status` 来自服务器响应的 HTTP 状态码 status: 200,
// `statusText` 来自服务器响应的 HTTP 状态信息 statusText: 'OK',
// `headers` 服务器响应的头 headers: {},
// `config` 是为请求提供的配置信息 config: {} }
|
拦截器
- axios.interceptors.request.use(function(拦截){ 操作 return 拦截})

请求拦截器
请求拦截器:在发起请求之前执行,可以对请求内容做修改,比如增加参数,设置请求头等等
axios.interceptors.request.use(function(config){ console.log("对请求config数据进行附加操作") console.log("url="+config.url); console.log("methods:"+config.method); config.url = config.url+"?token=xxx" console.log("url="+config.url); return config; },function(err){ console.log(err); })
|
应答拦截器
应答拦截器:服务器返回结果,axios的then之前先执行。可以对应答内容对预先的处理。
axios.interceptors.response.use(function(response){ console.log("应答拦截器,接收服务器返回的数据结构"); console.log(response); return response; },function(err){ console.log(err); })
|