网页第七讲(javascript概述)

三、基础知识

(一)数据类型

(二)变量

(三)关键字

(四)运算符

(五)对象

(六)函数

(七)语句

(八)事件处理

 

HTML 事件是发生在 HTML 元素上的事情。

  • HTML 页面完成加载
  • HTML 按钮被点击
  • HTML 输入框里的内容改变时

事件

描述

onchange

HTML 元素改变

onclick

用户点击 HTML 元素

onmouseover

用户在一个HTML元素上移动鼠标

onmouseout

用户从一个HTML元素上移开鼠标

onkeydown

用户按下键盘按键

onload

浏览器已完成页面的加载

如:

 

function over(){

//给按钮改个背景,#fff RGB=red g=green blue

//document.getElementById("btn").style.backgroundColor = "#f00";

//$("#btn") jquery.js在原生js上进一步封装后,简化代码编写

document.getElementById("btn").className = "overClass";

}

function out(){

//document.getElementById("btn").style.backgroundColor = "#0f0";

document.getElementById("btn").className="outClass";

}

function change(){/*当文本框内容发生改变的时候*/

var txt = document.getElementById("txt").value;

if(txt.length>10){

     alert("用户名长度不能超过10");

}

 

}

 

</script>

<style type="text/css">

.overClass{

background-color:#F90;

width:400px;

height:80px;

}

.outClass{

    background:#F60;/*background应用更广泛*/

width:200px;

}

</style>

</head>

 

<body onLoad="alert(6);"><!--场景:页面加载完毕时去调用后台,查询数据-->

<input type="button" id="btn" value="点我登陆" onClick="login('zq','123')"

      onMouseOver="over()" onMouseOut="out()"/>

<input type="text" id="txt" value="我是文本框" onKeyDown ="change()"/> <!--onFocus="alert('获取焦点');" onBlur="alert('我失去焦点了')"-->

 

<span id="msg"></span>

</body>

 

 

 

(九)其他对象( http://www.runoob.com/jsref/obj-window.html

  1. 浏览器对象:   

Window 对象表示浏览器中打开的窗口。

如果文档包含框架(<frame> 或 <iframe> 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外的 window 对象。

注意: 没有应用于 window 对象的公开标准,不过所有浏览器都支持该对象。

 

 

如:

例1:

alert(window.innerHeight+"---"+window.innerWidth);

window.close();//关闭当前的页面

 

例2:

alert(window.innerHeight+"---"+window.innerWidth);

function openNew(){

        window.open("http://www.baidu.com");//打开一个新页面

 

}

 

 

<input type="button" id="btn" value="点我登陆" onClick="openNew()"

      onMouseOver="over()" onMouseOut="out()"/>

 

      2.Location对象:

 

 

Location 对象包含有关当前 URL 的信息。

Location 对象是 window 对象的一部分,可通过 window.Location 属性对其进行访问。

注意: 没有应用于Location对象的公开标准,不过所有浏览器都支持该对象。

 

 

 

 

 

例如:

alert(window.innerHeight+"---"+window.innerWidth);

function openNew(){

window.location.href = "";//可以使用此种方法直接点出来location,以免打错了

location.href="http://www.baidu.com";

location.reload();//刷新当前页面

        //window.open("http://www.baidu.com");//打开一个新页面

 

}

 

        3.History对象:

 

History 对象包含用户(在浏览器窗口中)访问过的 URL。

History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问。

注意: 没有应用于History对象的公开标准,不过所有浏览器都支持该对象。

 

 

例如:

alert(window.innerHeight+"---"+window.innerWidth);

function openNew(){

window.history.back();

location.href="http://www.baidu.com";

history.back();//后退

history.forward();//前进

history.go(1);//1-前进一个页面 2 前进2个页面   -2  后退2个页面

//location.reload();//刷新当前页面

        //window.open("http://www.baidu.com");//打开一个新页面

 

}

 

 

 

 

(十)类型转换

 

var 声明的变量,没指明类型,如何查看类型?typeof 

一般数字和字符串、字符串和日期转换常用。

 

如:

 

function openNew(){

alert(typeof(String(123)));

alert(String(123+12.33));

var ss="555qq5";//字符串转数字

alert(parseInt(ss));//js只有这两种,没有parseDouble

alert(parseFloat("999"));//Integer.parseInt  Float.parseFloat  Double.parseDouble

}

 

 

(十一)JSON

 

JSON 是用于存储和传输数据的格式。通常用于服务端向网页传递数据

abebfc16cbf3170eea11fc6e0b3b690007f.jpg

 

1706fa2da45bc83a13b670750472a71b192.jpg

 

 

JSON 字符串转换为 JavaScript 对象 

var text = '{ "sites" : [' +

    '{ "name":"Runoob" , "url":"www.runoob.com" },' +

    '{ "name":"Google" , "url":"www.google.com" },' +

    '{ "name":"Taobao" , "url":"www.taobao.com" } ]}';

 

 

 

 

 

 

源代码:

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

<link type="text/css" rel="stylesheet" href="myCsss.css" />

<!--外链式  css引入时link是单标签的;成对的-->
<!--<script type="text/javascript" src="myJs.js"></script>-->

<!--内嵌式-->
<script type="text/javascript">
    //函数,和java的区别:参数无类型,返回值无类型
    /*
    void login(String username,String pwd)
    int login(String username,String pwd)
    String login(String username,String pwd)
    */
    function isExitName(username){//有返回值的方法
        if(username=='zq'){
            return true;    
        }else{
            return false;    
        }
    }
    function login(username,pwd){//单引号和双引号一样用
        //for in:遍历对象的属性
        var person = {
            height: "65kg",//属性
            weight: 165.00,
            sex: true,
            eat: function(){//方法
                alert("我吃饭了");
            },
            sleep: function(n){//n:睡了几个小时
                alert("睡了:"+n+"小时");
            }
        };
        for(var x in person){
            alert("属性是:"+x+"--属性值:"+person['height']//person.height
                +"--属性类型-"+typeof(person[x]));
        }
    
        //一个数组里面可以有多个类型的值,java里不可以int[] s=[]
        /*var s = [1,"hello",true,{},null];
        for(var i = 0; i<s.length; i++){
            alert(i+"---"+s[i]+"----"+typeof(s[i]));
        }*/
    
        /*switch(pwd){//case后面可以同时存在多种类型的数据,c和java不可以
            case 123://123==='123' case比较:数字和类型都要相等
                alert(123);
                break;
            case '123'://123==='123'
                alert("我是字符串123");
                break;
            case "hello":
                alert("hell");
                break;
            case true:
                alert(true);
                break;
            default:
                break;    
        }*/
        /*var isExit = isExitName(username);
        alert(isExit);
    
        if(pwd=='123' && isExit){
            alert("登陆成功");
        }else{
            alert("登录失败");
        }*/
    }
    
    /*var a = 123;
    var b = new Number(123);//int Integer
    alert(a===b); 
    alert(a==b);*/
    
/*    //Date
    var d = new Date();
    alert(d);
    alert(d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate()+
        " "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());//yyyy-MM-dd HH:mm:ss
    alert(d.getTime());//把日期直接转成long值,13位的数字,这个在php存储数据常用long值的日期
    alert(d.getDay());//日期,0-6,周日是0,周一是1,周六是6*/


    /*//String对象,和java的String类的方法几乎的一样的
    var a = "I am a china";    
    alert(a.length);//长度属性,算上空格
    alert(a.indexOf('c',4));//查找c字符的位置,第一个位置从0开始
    alert(a.lastIndexOf('c',20));//从后往前查c的位置,20超过字符串长度,不报错
    alert(a.substr(2,3));//截取字符串,从位置2开始截取,截取3个字符
    alert(a.substring(2,3));//从2开始截取,截取到第3个位置,不包含第3个位置的字符
    var b = a.split(" ");//以空格为分隔符,拆成数组
    alert(b[1]);//数组应该是这样的:["I","am","a","china"]*/
    
    //自定义一个对象
    /*var person = {
        height: "65kg",//属性
        weight: 165.00,
        sex: true,
        eat: function(){//方法
            alert("我吃饭了");
        },
        sleep: function(n){//n:睡了几个小时
            alert("睡了:"+n+"小时");
        }
    };
    alert("身高:"+person.height);//对象名字.属性
    alert("体重:"+person.weight);
    person.eat();//对象名字.方法或叫函数
    person.sleep("18个");*/

    /*//字符串+连接符
    var a = 12; 
    var b = "12";
    var c = a + b;
    alert(c);//1212
    var d = a + parseInt(b);//parseFloat,只有这2个
    alert(d);//24*/

    /*var a = 12;
    var b = "12";
    alert("==:"+(a==b));//只比较值是否相等
    alert(a===b);//值和类型都相等*/    
    
    /*var a = 1;
    var b = 1.0;
    b = "haha";//js里面可以赋值为不同类型是数据
    alert(typeof(b));//typeof是查看b的数据类型
    b = true;    
    alert(typeof(b));//typeof是查看b的数据类型

    var c;
    alert(typeof(c));//undefined
    var d = [];
    alert(typeof(d));//object对象类型
    */
    
    //几种住打印方式
    //console.log("我是console打印");
    //document.write('<h1>写入页面</h1>');
    //document.getElementById("msg").innerHTML = "您用户名不对";
    //alert("内嵌式引入js");
    /*
    多行注释
    */
    function over(){
        //给按钮改个背景,#fff RGB=red g=green blue
        //document.getElementById("btn").style.backgroundColor = "#f00";
        //$("#btn") jquery.js在原生js上进一步封装后,简化代码编写
        document.getElementById("btn").className = "overClass";
    }
    function out(){
        //document.getElementById("btn").style.backgroundColor = "#0f0";    
        document.getElementById("btn").className = "outClass";
    }
    function change(){//当文本框的内容发生改变的时候
        var txt = document.getElementById("txt").value;
        if(txt.length>10){
            alert("用户名长度不能超过10");
        }
    }
    //alert(window.innerHeight+"---"+window.innerWidth);
    function openNew(){
        var d = new Date();
        alert(typeof(d));
        alert(String(d));//转成字符串
        d.toString();//0字符串
        var s = "2019-12-12 12:12:12";//字符串变成日期类型
        new Date(s);
        /*alert(typeof(String(123)));
        alert(String(123+12.33));
        var ss = "qq555qq5";//字符串转数字
        alert(parseInt(ss));//js只有这两种,没有parseDouble
        alert(parseFloat("999"));//Integer.parseInt  Float.parseFloat  Double.parseDouble
        /*window.location.href="";//可以用此种方法直接点出来location,以免打错了
        window.history.back();
        //window.document;//
        location.href="http://www.baidu.com";
        history.back();//后退
        history.forward();//前进
        history.go(1);//1-前进一个页面 2前进2个页面  -2 后退2个页面
        //location.reload();//刷新当前页面
        //window.open("http://www.baidu.com");//打开一个新页面
        //window.close();//关闭当前的页面*/
    }
</script>
</head>

<body onLoad="alert(6);"><!-- 场景:页面加载完毕时去调用后台,查询数据 -->
<input type="button" id="btn" value="点我登陆" onClick="openNew()"
      onMouseOver="over()" onMouseOut="out()"/>
<input type="text" id="txt" value="我是文本框" onKeyPress="change()" /><!-- onChange="change()" onFocus="alert('获取焦点');"     onBlur="alert('我失去焦点了')"  -->
<span id="msg"></span>
</body>
<style type="text/css">
    .overClass{
        background-color:#F90;
        width:400px;
        height:80px;        
    }
    .outClass{
        background:#F60;/*background应用更广泛*/
        width:200px;
    }
</style>
</html>

 

转载于:https://my.oschina.net/u/4090401/blog/3044045

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值