分享

javascript

 心静水境 2018-03-22
/**
 * Created by Administrator on 2017/9/27.
 */
name='zhangmeide';//全局变量
var name='zhangmeide';//局部变量
a='12'
num=parseInt(a);//字符串转数字整形
num=parseFloat(a)//字符串转数字浮点数
a.charAt(索引位置)
// a.substring(起始位置,结束位置)
// a.length 获取当前字符串长度

function f1() {//function定义函数,f1函数名
    // body...
    console.log(1);//控制台console打印
}
function f2() {//function定义函数,f1函数名
    // body...
    var tag=document.getElementById('i1');//根据ID获取指定标签的内容
    var content=tag.innerText;//获取标签内部的内容
    var f=content.charAt(0);
    var l=content.substring(1,content.length);
    var new_content=f+l;
    tag.innerText=new_content;
}
setInterval("f2()",2000);//创建一个定时器,每2秒执行一次"f1()"语句  f1()

a=[11,22,33]//数组
a={'k1':'v1','k2':'v2'}
for(var item in a){
    a[item];//item为a的索引值   数组和字典都为索引
}
for(var i=0;i<10;i++){//i++相当于i=i+1
    i;
}
for(var i=0;i<a.length;i=i+1){
    a[i];
}
if(1=="1" && 2=="2"){//值相等

}
if(1===1 || 2===2){//值和类型都相等

}
if(1!==1){

}
var tag=document.getElementById('i1')
document.getElementsByTagName('a')
document.getElementsByClassName()
tag.parentElement()
tag.children
tag.className
tag.classList.add('添加样式名')
tag.classList.remove('删除样式名')
JSON.stringify(obj)//将对象转化为字符串
JSON.parse('字符串')//将字符串转化为对象类型
//作用域
function func(){
    var xxoo='alex';
    console.log(xxoo);
    var xxoo='zhangmeide';
}
func();//输出alex

function func(){
    console.log(xxoo);
    var xxoo='zhangmeide';
}
func();//undefined xxoo没有定义

function func(){
    console.log(xxoo);
}
func();//报错

//面向对象

function Foo(n) {
    this.name = n;//this代指对象,相当于python中的self
}
var obj=new Foo('zhangmeide');//创建一个对象

Foo.prototype={   //原型
    'sayName':function (){
        console.log(this,name)
    }
}

var obj=document.getElementById('i1');
obj.innerText; //仅文本
obj.innerHTML;//全部内容
obj.value;//input,select(obj.selectedIndex),textarea的内容
obj.setAttribute('class','alex');//添加属性
obj.removeAttribute('value');//删除属性
obj.attributes;//查看所有属性
obj.submit;//提交表单
obj.onmouseover//鼠标停留
obj.onmouseout//鼠标不在

var v=confirm('提示信息');//v:true false

location.href;//获取当前url
location.href="https://www.baidu.com";//重定向 跳转
location.reload();//页面刷新 相当于location.href=location.href

var obj1=setInterval(function () {//定时器
    console.log(1);
},5000);
clearInterval(obj);//清空定时器
var obj2=setTimeout(function () {//定时器 只执行一次
    console.log(1);
},5000);
clearTimeout(obj2);//清空定时器

var myTrs=document.getElementsByName('tr');
var len=myTrs.length;
for(var i=0;i<len;i++){
    myTrs[i].onmouseover=func(){
        this.style.background='red';//this代指myTrs[i].onmouseover事件
    };
}

obj.addEventListener("click",function () {
    console.log("content")
},false)

function t1(arg){
    console.log(arg);
    var arg=27;
    console.log(arg);
    function arg(){}
    console.log(arg);
}
t1(3)
active object==========>AO
1.形式参数
2.局部变量
3.函数声明表达式

1.形式参数
AO.age=undefined
AO.age=3
2.局部变量
AO.age=undefined
3.函数声明表达式
AO.age=function ()

JS正则:
    1.test -判断字符串是否符合规定的正则
    2.exec -获取匹配的数据
var rep=/\d+/;//相当于pyhon中的rep='\d+'
rep.test('str要匹配的字符串123')

var rep=/(\d+)/;//分组匹配
var rep=/(\d+)/g;//全局匹配
var rep=/(\d+)/i;//不分大小写匹配
var rep=/(\d+)/m;//多行匹配 把\n换行符的字符串拆分为多个字符串匹配 针对于^开头符
rep.exec('str要匹配的字符串123')

jQuery Ajax操作
jQuery.get({
    url:'data',
    type:'get',
    dataType:'text',
    success:function (data,text5status,xmlHttpResponse) {
        console.log(data);
        console.log(text5status);
        console.log(xmlHttpResponse);
    }
});
jQuery.post()
jQuery.getJSON()
jQuery.getScript()
jQuery.ajax({
    url:'data',
    type:'get',
    headers:'',
    data:'',
    contentType:'',
    async:'',

    beforeSend:'',
    complete:'',
    success:'',
    error:'',

    accepts:'',
    dataType:'text',
    converters:'',

    success:function (data,text5status,xmlHttpResponse) {
        console.log(data);
        console.log(text5status);
        console.log(xmlHttpResponse);
    }

原生Ajax 状态码系列:
Text:xhr.responseText
xml:xhr.responseXML
json:JSON.parse(xhr.responseText)//字符串转换成数组或字典
script:eval(xhr.responseText)//字符串转换成script代码并执行

img src iframe script....可以跨域
jsonp实行跨域
response['Access-Control-Allow-Origin']='*'//所有网站允许跨域请求

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多