JavaScript快速入门

1.什么是JavaScript

2.引入方式

3.基础语法

(1)变量&常量

(2)输出语句

(3)数据类型

注意:模版字符串的字符串拼接

for example:

    //1. 数据类型
    // alert(typeof 10); //number
    // alert(typeof 1.5); //number
    
    // alert(typeof true); //boolean
    // alert(typeof false); //boolean

    // alert(typeof "Hello"); //string
    // alert(typeof 'JS'); //string
    // alert(typeof `JavaScript`); //string

    // alert(typeof null); //null ? -> object

    // let a ;
    // alert(typeof a); //undefined

    //2. 模板字符串 - 简化字符串拼接
    let name = 'Tom';
    let age = 18;

    console.log('我是'+name+', 我今年'+age+'岁');
    console.log(`我是${name}, 我今年${age}岁`);
    

(4)函数

<1>具名函数

    // 函数定义及调用  -具名函数
    let sm = add(10, 20);
    alert(sm)

    function add(a, b) {
        return a + b;
    }
<2>匿名函数

函数表达式:

    // 函数定义及调用  -匿名函数
    // 函数表达式方式
    let sum = function (a, b) {
        return a + b;
    };
    let ans = sum(10, 20);
    alert(ans);

箭头函数:

    // 箭头函数
    let sum = (a, b) => {
        return a + b;
    }
    let ans = sum(10, 20);
    alert(ans);

(5)自定义对象

    // 1.自定义对象
    // let user = {
    //     name: '张三',
    //     age: 18,
    //     gender: '男',
    //     sing() {
    //         alert(this.name + '唱着最炫的民族风');
    //     }
    // }

    let user = {
        name: '张三',
        age: 18,
        gender: '男',
        sing: function () {
            alert(this.name + '唱着最炫的民族风');
        }
    }
    // 2.调用对象的属性和方法
    alert(user.name);
    alert(user.age);
    alert(user.gender);
    user.sing();

(6)json与对象的转换

stringify:

    // 3.JSON   -JS对象标记法
    let person = {
        name: '张三',
        age: 18,
        gender: '男'
    }
    alert(JSON.stringify(person));  // 将person对象转为JSON字符串

parse:

    let personJson = '{"name":"张三","age":"18","gender":"男"}';
    alert(personJson);
    alert(JSON.parse(personJson).name);  // 将JSON字符串转为对象

4.DOM

什么是DOM

DOM操作

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS-DOM</title>
</head>
<body>

<h1 id="title1">11111</h1>
<h1>22222</h1>
<h1>33333</h1>

<script>
    // 1.修改第一个h1标签的文本内容
    // 1.1获取DOM对象
    // let h1 = document.querySelector('#title1');
    let h1 = document.querySelector('h1');
    let hs = document.querySelectorAll('h1');


    //1.2调用DOM对象的属性或者方法
    h1.innerHTML = '修改后的文本内容';
    hs[2].innerHTML = '修改后的文本内容';

</script>
</body>
</html>

5.事件与事件监听

事件:

事件监听:

for example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS事件</title>
</head>
<body>

<input type="button" id="btn1" value="点我一下试试1">
<input type="button" id="btn2" value="点我一下试试2">

<script>

    // 事件监听   可以多次绑定同一事件
    document.querySelector('#btn1').addEventListener('click', () => {
        alert('点击了按钮1');
    })
    document.querySelector('#btn1').addEventListener('click', () => {
        alert('点击了按钮1。。。。');
    })
    document.querySelector('#btn2').addEventListener('click', () => {
        alert('点击了按钮2');
    })

</script>
</body>
</html>

案例引入(鼠标进入某行,某行变色,离开变回原色):

    // 1.获取所有tr标签
    let trs = document.querySelectorAll('tr');

    // 2.为每一个tr标签添加鼠标进入和离开事件监听
    for (let i = 0; i < trs.length; i++) {
        trs[i].addEventListener('mouseenter', function () {
            this.style.backgroundColor = '#f2e2e2'; // mouseenter 鼠标进入
        });
        trs[i].addEventListener('mouseleave', function () {
            this.style.backgroundColor = '#fff';  // mouseleave 鼠标离开
        });
    }

常见事件:

代码如下:

        //click: 鼠标点击事件
        document.querySelector('#b2').addEventListener('click', () => {
            console.log("我被点击了...");
        })

        //mouseenter: 鼠标移入
        document.querySelector('#last').addEventListener('mouseenter', () => {
            console.log("鼠标移入了...");
        })

        //mouseleave: 鼠标移出
        document.querySelector('#last').addEventListener('mouseleave', () => {
            console.log("鼠标移出了...");
        })

        //keydown: 某个键盘的键被按下
        document.querySelector('#username').addEventListener('keydown', () => {
            console.log("键盘被按下了...");
        })

        //keyup: 某个键盘的键被抬起
        document.querySelector('#username').addEventListener('keyup', () => {
            console.log("键盘被抬起了...");
        })

        //blur: 失去焦点事件
        document.querySelector('#age').addEventListener('blur', () => {
            console.log("失去焦点...");
        })

        //focus: 元素获得焦点
        document.querySelector('#age').addEventListener('focus', () => {
            console.log("获得焦点...");
        })

        //input: 用户输入时触发
        document.querySelector('#age').addEventListener('input', () => {
            console.log("用户输入时触发...");
        })

        //submit: 提交表单事件
        document.querySelector('form').addEventListener('submit', () => {
            alert("表单被提交了...");
        })

注意模块化的js:

(1)将打印方法与弹窗方法提取出来

注意:要把方法暴露出来

// 注意:要使用 export 才能被其他文件引用
export function printLog(msg) {
    console.log(msg);
}

export function alertMsg(msg) {
    alert(msg);
}

(2)将js提取出来

注意:导入utils中方法

// 引入utils模块
import {printLog, alertMsg} from "./utils.js";


//click: 鼠标点击事件
document.querySelector('#b2').addEventListener('click', () => {
    // console.log("我被点击了...");
    printLog("我被点击了...")
})

//mouseenter: 鼠标移入
document.querySelector('#last').addEventListener('mouseenter', () => {
    printLog("鼠标移入了...");
})

//mouseleave: 鼠标移出
document.querySelector('#last').addEventListener('mouseleave', () => {
    printLog("鼠标移出了...");
})

//keydown: 某个键盘的键被按下
document.querySelector('#username').addEventListener('keydown', () => {
    printLog("键盘被按下了...");
})

//keyup: 某个键盘的键被抬起
document.querySelector('#username').addEventListener('keyup', () => {
    printLog("键盘被抬起了...");
})

//blur: 失去焦点事件
document.querySelector('#age').addEventListener('blur', () => {
    printLog("失去焦点...");
})

//focus: 元素获得焦点
document.querySelector('#age').addEventListener('focus', () => {
    printLog("获得焦点...");
})

//input: 用户输入时触发
document.querySelector('#age').addEventListener('input', () => {
    printLog("用户输入时触发...");
})

//submit: 提交表单事件
document.querySelector('form').addEventListener('submit', () => {
    alertMsg("表单被提交了...");
})

(3)配置提取目录以及模块化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值