JavaScript知识汇总集合

1.输出

document.write(“hello word”);//如果在文档加载完成后,此时输出,会覆盖整个文档
从外部引入js文件时,在js文件内不用书写

2.局部变量,全局变量

var m=5,n=10;//全局变量 function demo(){ var i=15;//局部变量 x=8;//当demo函数被调用时,x为全局变量,否则x不能被使用 }

3.事件

onClick 单击事件click
onMouseOver 鼠标经过事件mouseover
onMouseOut 鼠标移出事件
onChange 文本内容改变事件
onSelect 文本框选中事件
onFocus 光标聚集事件
onBlur 移开光标事件
onLoad 网页加载事件
onUnload 关闭网页事件

4.使用DOM操作HTML

(1)寻找元素
    通过id找到HTML元素:document.getElementById("pId")
    通过标签名找到HTML元素:document.getElementByTagName("p")//当出现多个多个相同的p标签,它会得到相同元素的第一个
(2)改变HTML内容
    使用属性:innerHTML   document.getElementById("pId").innerHTML="Hello"
(3)改变HTML属性
    使用属性:attribute
    <a id="aId" href="http://www.baidu.com">helloword</a>
    document.getElementById("aId").href="http://www.jikexueyuan.com";//改变href属性的内容

5.使用DOM对象操作CSS

(1)语法:document.getElementById(id).style.property=new style
<div id="divId" class="divClass">helloword</div>
.divClass{
    width:20px;
    hight:20px;
    background:red; 
}
document.getElementById("divId").background="blue";//改变divId中的背景样式

6.DOM EventListener:添加事件句柄

方法:addEventListener();//用于向指定元素添加事件句柄
    removeEventListener();//移出事件的句柄
<button id="btn">按钮</button>
document.getElementById("btn").addEventListener("click",function(){alert("hello");});
//句柄事件可以添加多个,不会冲突
var x=document.getElementById("btn");
x.addEventListener("click",hello);
x.addEventListener("click",word);
x.removeEventListener("click",hello);
function hello(){
    alert("hello");
}
function word(){
    alert("word");
}

7.JaveScript事件详解

(1)事件流:描述的是在页面中接受事件的顺序
1.事件冒泡:由最具体的元素接收,然后逐级向上传播至最不具体的元素的节点(文档)
2.事件捕获:最不具体的节点先接收事件,而最具体的节点应该是最后接收事件
<div id="div">
    <button id="btn">按钮</button>
</div>//事件冒泡顺序:先button 后div;事件捕获顺序:先div 后button
(2)事件处理:
1.HTML事件处理:直接添加到HTML结构中,如<button onclick="demo()">按钮</button>
2.DOM0级事件处理:把一个函数赋值给一个事件处理程序属性
        如:<button id="btn">按钮</button>
            <script>
                var btn=document.getElementById("btn");
                btn.onClick=function(){alert("DOM0级事件处理:");}//添加DOM0级事件处理
                btn.onClick=function(){alert("测试事件覆盖");}//DOM0级事件处理,存在事件冲突,多个只会执行最后一个
                btn.onClick=null;//清除事件
3.DOM2级事件处理:
    addEventListener("事件名","事件处理函数","布尔值");其中布尔值true:事件捕获,false:事件冒泡
    removeEventListener;

4.IE事件处理程序(IE版本小于8)
    attachEvent
    detachEvent
5.浏览器事件支持,根据浏览器支持事件的类型,选择设置事件
    <script>
        var btn=document.getElementById("btn");
        if(btn.addEventListener){
        btn.addEventListener("click",demo);
        }else if(btn.attachEvent){
        btn.attachEvent("onclick",demo);
        }else{
        btn.onclick=demo();
        }
(3)事件对象event:在触发DOM事件的时候都会产生一个对象
    1.type:获取事件类型
    2.target:获取事件目标
    3.stopPropagation():阻止事件冒泡
    4.preventDefault():阻止事件默认行为
        <div id="div">
            <button id="btn">按钮</button>
            <a id="aid" href="www.baidu.com">百度</a>
        </div>
        <script>
            document.getElementById("btn").addEventListener("click",showType);      
            document.getElementById("div").addEventListener("click",showDiv);
            document.getElementById("aid").addEventListener("click",showA);
        function showType(event){
            alert(event.type);//获得事件类型
            alert(event.taarget);//获得响应事件的目标
            event.stopPropagation();//阻止事件冒泡
        }
        function showDiv(){
            alert("div");           
        }
        function showA(event){
            event.stopPropagation();//阻止事件冒泡
            event.preventDefault();//阻止a标签中href默认跳转事件
        }
        </script>

8.JS内置对象:String字符串对象、Date日前对象、Array数组对象、Math对象、浏览器对象

    //创建对象:方法一
    people =new Object();
    people.name="wusw";
    people.age="23";
    document.write("name:"+people.name+" age:"+people.age);
    //创建对象:方法二
    people={name:"wusw",age:"23"};
    document.write("name:"+people.name+" age:"+people.age);
    //创建对象:方法三,使用函数创建
    function people(name,age){
    this.name_=name;
    this.age_=age;
    }
    son=new people("wusw",23);
    document.write("name:"+son.name+" age:"+son.age);
(1)String字符串对象:可以使用双引号或单引号
    match("ad")  内容匹配(有该内容时,返回该内容,没有返回null)
(2) Date对象
    getFullYear()   获得年份
    getTime()   获取毫秒
    setFullYear()   设置具体的日期
    getDay()    获取星期
    getHours()  获得小时
    getMinutes()    获得分
    getSenconds()   获得秒
    var date=new date();
    document.write(date);
    document.write(date.getFullYear());
    document.write(date.getTime());
    date.setFullYear(2015,7,1);
    document.write(date);

    <body onload="startTime()">//onload方法表示页面加载时执行开始计算方法
    <script>
        function startTime(){
        var today=new date();
        var h=today.getHours();
        var m=today.getMinutes();
        var s=today.getSenconds();
        m=checkTime(m);
        s=checkTime(m);
        document.getElementById("textId").innerHtml=h+":"+m+":"+s;
        t=setTimeOut(function(){
                startTime();
        },1000);//自动更新
    }
    function checkTime(i){
        if(i<10){
        i="0"+i;
        }
        return i;

    }
    </script>
    <div id="textId"></div>
    </body>
(3)Array数组对象
    concat()    合并数组
    sort()      排序
    push()      末尾追加元素
    reverse()   数组元素翻转
    <script>
        var a={"5","4","3","2","1"};
        doucment.write(a.sort());//升序排列
        document.write(a.sort(function(a,b){
        //return a-b;//升序
        return b-a;//降序
        }));

        </script>
(4)Math对象
    round()     四舍五入
    random()    返回0~1之间的随机数
    max()       返回最高值
    min()       返回最低值
    abs()       返回绝对值

9.JavaScript DOM对象控制HTML

    getElementsByName() 获取name
    getElementsByTagName()  获取元素
    getAttribute()      获取元素属性
    setAttribute()      设置元素属性
    childNodes()        访问子节点
    parentNode()        访问父节点
    createElement()     创建元素节点
    createTextNode()    创建文本节点
    insertBefore()      插入节点
    removeChild()       删除节点
    offsetHeight()      网页尺寸(不包含滚动条)
    scrollHeight()      网页尺寸(包含滚动条)

hello

hello

hello

hello

hello

得到父节点

  • 1
  • 2
  • 3
function getName(){ var count=document.getElementsByName("pn");//当有多个相同的name时,它返回是这多个的集合 alert(count.length); var p=count[2]; p.innerHTML="World"; } getName(); function getTagName(){ var count=document.getElementsByName("p");//通过具体元素名获取,当有多个时,它返回是这多个的集合 alert(count.length); var p=count[2]; p.innerHTML="World"; } getTagName(); function getAttr(){ var anode=document.getElementById("aid");//通过 a标签 var attr=anode.getAttribute("title");//通过a元素的title属性,得到title属性的值,也可以通过id属性获得id属性的值 document.write(attr); } getAttr(); function setAttr(){ var anode=document.getElementById("aid");//通过 a标签 var attr=anode.setAttribute("title","动态设置title属性的值");//第一个参数:属性,第二个参数:属性的值 var getAttr=anode.getAttribute("title");/ document.write(getAttr); } setAttr(); function getChildNode(){ var childnode=document.getElementsByName("ul")[0].childNodes;//多个返回的是数组 alert(childnode.length);//标签与标签直接的空白处算一个 alert(childnode[0].nodeType); } getChildNode(); function getParentNode(){ var div=document.getElementById("pid"); alert(div.getParentNode.nodeName); } getParentNode(); function createNode(){ var body=document.body; var input=document.createElement("input"); intput.type="button"; input.value="按钮"; body.appendChild(input);//将元素节点添加到body中 } createNode(); function addNode(){ var div=document.getElementById("divId"); var pnode=document.getElementById("pid"); var newnode=document.createElement("p"); newnode.innerHTML="动态添加一个P元素,将其放在特定标签之前"; div.insertBefore(newnode,pnode); } addNode(); function removeNode(){ var div=document.getElementById("divId"); var p=div.removeChild(div.getChildNodes[1]); } removeNode(); function getSize(){ var width=document.body.offsetWidth||document.documentElement.offsetWidth; var height=document.body.offsetHeight||document.documentElement.offsetHeight; alert("宽度:"+width+",高度:"+height); } getSize();

10、window对象,计时器,History对象,location对象,screen对象

(1)window对象时BOM的核心,window对象指当前的浏览器窗口所有JavaScript全局对象、函数以及
    变量均自动成为window对象的成员,其中全局变量时window对象的属性,全局函数式window对象的方法。
    甚至HTML DOM的document也是window对象的属性之一
    window尺寸:
        window.innerHeight 浏览器窗口的内部高度
        window.innerWidth  浏览器窗口的内部宽度
    window方法:           window.open()   打开新窗口 window.open("newIndex.html","网页标题名称","height=200,width=200,left=100,top=100",true)
        window.close()  关闭窗口
(2)计时器:
    setInterval()   间隔指定的毫秒数不停地执行指定的代码
    clearInterval() 用于停止setInterval()方法执行的代码
    setTimeout()    暂停指定的毫秒数后执行指定的代码
    clearTimeout()  停止setTimeout方法 
<p id="ptime"></p>
<input type="button" value="暂停" onclick="stopTime()"></input>
<input type="button" value="取消" onclick="clearTimeout()"></input>
<script>
var mytime=setInterval(function(){
    getTime();
},1000);
function getTime(){
var time=new Date();
var t=time.toLocalTimeString();
document.getElemantById("ptime").innerHTML=t;   
}
function stopTime(){
    clearInterval(mytime);//停止之前创建的定时器
}
var timeOut;
function myTimeOut(){
    timeOut;=setTimeOunt(function(){
    alert("timeout");
    },1000);    
}
function clearTimeout(){
    clearTimeout(timeOut);
    }
</script>
(3)History对象:window.history对象包含浏览器的历史(url)的集合
    history.back() 与在浏览器点击后退按钮相同
    history.forward() 与在浏览器点击向前按钮相同
    history.go()      进入历史中的某个界面
(4)location对象:用于获得当前的地址(url),并把浏览器重定向到新的页面
    location对象的属性:
        location.hostname   返回web主机的域名
        location.pathname   返回当前页面的路径和文件名
        location.port       返回web主机的端口
        location.protocol   返回所使用的web协议(如http://或https://)
        location.href       属性返回当前页面的URL
        location.assign()   加载新的文档如:window.location.assign("http://www.baidu.com");
(5)screen对象:window.screen对象包含有关用户屏幕的信息
        screen.availWidth   可用的屏幕宽度
        screen.availHeight  可用的屏幕高度
        screen.Height       屏幕高度
        screen.Width        屏幕宽度

11.javascript面向对象

(1)认识对象:
    var person={
        name:"wusw",
        age:30,
        eat:function(){
            alert("能吃");
        }
    }   
    alert(person.name)
(2)使用函数构造器生产对象
    function Person(){//通过function来虚拟创建一个Person的类

    }   
    Person.prototype={//通过prototype可以设置Person对象中的属性和方法
     name:"iwen",
    age:30,
    eat:function(){
        alert("能吃")
        }
    }
    var p=new Person();//声明一个Person对象
    alert(p.name);//调用对象的方法
(3)深入了解javascript面向对象
    声明:
        function People(){

        }
        People.prototype.say=function(){//通过prototype创建一个say方法
        alert("hello");
        }
    声明2:
        function Person(){
            var _this={}//创建一个空的对象
            _this.say=new function(){
            alert("hello");
            }
            return _this;
        }
    继承:

        function Student(){

        }
        Student.prototype=new People();
        var s=new Student();
        s.say();
    继承2:
        function Teacher(){
            var _this=Person();
            return _this;   
        }
        var t=Teacher();
            v.say();
    重写:
        Student.prototype.say=function(){
            alert("stu-hello");
        }       
        var s=new Student();
        s.say();
    重写2:
        function Teacher(){
            var _this=Person();
            _this.say=new function(){
                alert("T-hello");
            }
            return _this;   
        }
        var t=Teacher();
            v.say();
    调用父类的方法:
        var superSsay=Student.prototype.say;
        function superSay(){
        superSsay.call(this);
        }
        superSay();
    第二种方式创建的对象,调用父类的方法:
        function Teacher(){
            var _this=Person();
            var superSay=_this.say;

            _this.say=new function(){
                superSay.call(_this);
                alert("T-hello");
            }
            return _this;   
        }
        var t=Teacher();
            v.say();
    传递参数:
        function Perple(name){
            this._name=name;
        }
        People.prototype.say=function(){
            alert("peo-hello:"+this._name);
        }
        function Student(name){
        this._name=name;
            }
        Student.prototype=new Perple();
        var superSay=Student.prototype.say;
        Student.prototype.say=function(){
        syperSay.call(this);
        alert("stu-hell0:"+this._name);
        }
        var s=new Student("wusw");
            s.say();
    第二中方法创建的对象,传递参数:
        function Person(name){
            var _this={}//创建一个空的对象
            _this._name=name;
            _this.say=new function(){
            alert("hello:"+_this._name);
            }
            return _this;
        }
        function Teacher(name){
            var _this=Person();
            _this.say=new function(name){
                alert("T-hello:"+_this.name);
            }
            return _this;   
        }
        var t=Teacher("wusw");
            v.say();
    信息封装:
        (function(){
            var n="wusw";//此时的n只能被该函数内调用
            function Perple(name){
            this._name=name;
            }
            People.prototype.say=function(){
            alert("peo-hello:"+this._name);
            }
            window.People=People;//向外部提示可以调用该对象的方式
        }());
    第二种声明对象的封装:
        (function(){
            var n="wusw";
            function Person(name){
            var _this={}//创建一个空的对象
            _this._name=name;
            _this.say=new function(){
            alert("hello:"+_this._name);
            }
            return _this;
            }
            window.Person=Person;
        }());
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值