javascript基础

javascript

01-javascript介绍

image-20250914221618002

image-20250914221944764

image-20250914222248205

image-20250914222457397

image-20250914222612855

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .pink {
            background-color: pink;
        }
    </style>
</head>
<body>
    <button class="pink">按钮1</button>
    <button>按钮2</button>
    <button>按钮3</button>
    <button>按钮4</button>
    <script>
        let bts = document.querySelectorAll('button')
        for (let i = 0; i < bts.length; i++){
            bts[i].addEventListener('click', function(){
                document.querySelector('.pink').className = ''
                this.className = 'pink'
            })
        }
    </script>
</body>
</html>

以上代码实现点击哪个按钮, 哪个按钮的颜色变为粉色

image-20250914225157041

02-javascript书写位置

image-20250914225327048

01-内部js

image-20250914225400827

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!-- 内部js-->
    <script>
        // 页面弹出警示框
        alert('hello, js')
    </script>
</body>
</html>

image-20250914230017080

image-20250914230202026

02-外部js

image-20250914231225488

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <script src="index.js"></script>
</body>
</html>

03-内联js

image-20250914231727165

image-20250914231806481

03-javascript注释和结束符

image-20250914232041521

image-20250914232103956

image-20250914232232665

image-20250914232424177

image-20250914232455908

04-js 输入和输出语句和字面量

image-20250914232542700

image-20250914232643623

image-20250914233146875

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <script>
    // 1.文档输出内容
    document.write("hello, this is a output");
    document.write('<h1> hello, js</h1>')
  </script>
</body>
</html>

image-20250914233219101

image-20250914233301909

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <script>
    // 1.文档输出内容
    document.write("hello, this is a output");
    document.write('<h1> hello, js</h1>');
    console.log("控制台输出!");
  </script>
</body>
</html>

image-20250914233500343

image-20250914233612701

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <script>
    // 1.文档输出内容
    document.write("hello, this is a output");
    document.write('<h1> hello, js</h1>')
    console.log("控制台输出!")
    // 输入语句
    prompt("请输入您的喜好:")
  </script>
</body>
</html>

image-20250914233725301

image-20250914233928000

image-20250914233956070

image-20250914234052578

js 书写位置: 标签内联, script 标签内部, script 标签引用外部js 文件

image-20250914234305740

05-变量的声明和赋值

image-20250915221553435

image-20250915221659434

image-20250915221750001

image-20250915221840203

image-20250915221930261

image-20250915222148582

image-20250915222302583

image-20250915222442998

image-20250915222518129

06-变量的更新以及输入用户名案例

image-20250915222922024

image-20250915223131071

image-20250915223204234

image-20250915223448155

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <script>
      let uname = prompt("请输入姓名");
      document.write(uname);
  </script>
</body>
</html>

image-20250915223823665

07-交互两个变量案例

image-20250915223838620

image-20250915224017811

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <script>
      let num1 = 10;
      let num2 = 20;
      let temp = num1;
      num1 = num2;
      num2 = temp;
      console.log(num1, num2);
  </script>
</body>
</html>

08-变量的本质和命名规则

image-20250915224557104

image-20250915224836524

image-20250915225155767

image-20250915225333077

image-20250915225431829

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <script>
      let username = prompt("please input your name");
      let age = prompt("please input your age");
      let gender = prompt("please input your gender");
      document.write(username);
      document.write(age);
      document.write(gender);
  </script>
</body>
</html>

09-var 和 let 区别

image-20250915230024940

image-20250915230317137

10-数组的基本使用

image-20250915230402328

image-20250915230540402

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <script>
      let arr = ['嘿嘿', "哈哈", "啦啦"];
      document.write(arr[1]);
  </script>
</body>
</html>

image-20250915230952463

image-20250915231057567

image-20250915231250597

image-20250915231505331

11.常量

image-20250920114942178

image-20250920115255013

12.数字数据类型和算术运算符

image-20250920115416923

image-20250920115524837

image-20250920115550643

image-20250920115836668

image-20250920115911767

image-20250920120201182

image-20250920120302485

image-20250920120619201

13.字符串数据类型及拼接

image-20250920121015914

image-20250920121612808

14.模版字符串

image-20250920121855234

image-20250920121933708

15.布尔型、null、undefined、类型检测

image-20250920122531362

image-20250920122737138

image-20250920122956120

image-20250920123157623

image-20250920123245911

image-20250920123403513

image-20250920123628317

16.隐式转换和显示转换

image-20250920123927722

image-20250920124429285

image-20250920124735771

image-20250920125255911

17.小案例

image-20250920130905961

image-20250920132037348

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .center {
            text-align: center;
        }
        table {
             /*合并相邻边框*/
            border-collapse: collapse;
            height: 80px;
            margin: 0 auto;
            text-align: center;
        }
        th {
            padding: 5px 30px;
        }
        table,
        th,
        td {
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <h2 class="center">订单确认</h2>

  <script>
      // 用户输入
      let price = +prompt('请输入商品价格:')
      let num = +prompt('请输入商品数量:')
      let address = prompt('请输入收获地址:')
      // 计算总额
      let total = price * num
      // 页面打印渲染
      document.write(`
        <table>
            <tr>
                <th>商品名称</th>
                <th>商品价格</th>
                <th>商品数量</th>
                <th>总价</th>
                <th>收获地址</th>
            </tr>
            <tr>
                <td>xx手机</td>
                <td>${price}元</td>
                <td>${num}</td>
                <td>${total}元</td>
                <td>${address}</td>
            </tr>
        </table>
      `)
  </script>
</body>
</html>

image-20250920132306709

image-20250920132330845

image-20250920132400072

image-20250920132427162

image-20250920132533142

18.赋值运算符

image-20250920145016214

image-20250920145149167

image-20250920145503323

image-20250920145548614

19.自增运算符

image-20250920145831362

image-20250920145921370

image-20250920150133545

image-20250920150419347

image-20250920150521815

image-20250920150611066

image-20250920150926907

上图的 结果是 7

20.比较运算符

image-20250920151033298

image-20250920151052422

image-20250920151205683

image-20250920151625323

image-20250920151916820

image-20250920151958038

21.逻辑运算符及其优先级

image-20250920152105373

image-20250920152209807

image-20250920152434804

image-20250920152506819

image-20250920153037633

image-20250920153503267

上图d 的结果是 true

22.if 语句

image-20250920153652798

image-20250920153721256

image-20250920153808376

image-20250920153848492

image-20250920153925328

image-20250920154107965

image-20250920154210360

image-20250920154238579

image-20250920154742928

image-20250920155126955

image-20250920155216767

image-20250920155642210

image-20250920155813489

image-20250920155938187

image-20250920160248208

23.三元运算符

image-20250920162057159

image-20250920162147579

image-20250920162347187

image-20250920162536510

image-20250920162730269

image-20250920162825444

image-20250920163259941

24.switch 分支语句

image-20250920163520983

image-20250920163807786

image-20250920164028677

image-20250920164409893

25.断点调试

image-20250920164635866

image-20250920164926203

image-20250920165006910

image-20250920165213305

26.while 循环

image-20250920165639116

image-20250920165719712

image-20250920170030209

image-20250920170104143

image-20250920170819257

image-20250920170803732

27.break、continue

image-20250920171105334

image-20250920171035704

image-20250920171211052

image-20250920171304596

image-20250920171404241

image-20250920171528355

28.for 循环

image-20250920172029870

image-20250920184810490

image-20250920184919883

image-20250920185424257

image-20250920185641920

image-20250920185753150

image-20250920185826240

image-20250920190045934

image-20250920190238352

image-20250920190716334

image-20250920190822091

image-20250920190948507

image-20250920191101957

29.循环嵌套

image-20250920191225493

image-20250920191406020

image-20250920191633641

image-20250920191946822

image-20250920192127901

image-20250920192206791

image-20250920192243856

image-20250920192524581

30.数组

image-20250920192702256

image-20250920192807760

image-20250920192957732

image-20250920193033714

image-20250920193109476

image-20250920193139221

image-20250920193211604

image-20250920193557800

image-20250920193650091

image-20250920194107545

image-20250920194431806

image-20250920194632726

image-20250920195026991

image-20250920195539089

image-20250920195617592

image-20250920195731740

image-20250920195912890

image-20250920200000439

image-20250920200026129

image-20250920200055208

image-20250920200223914

image-20250920200323708

image-20250920200631031

image-20250920200427376

image-20250920200715694

image-20250920200814811

image-20250920200903074

image-20250920201040022

31.函数

image-20250920201510173

image-20250920201630702

image-20250920201933233

image-20250920201909620

image-20250920202039977

image-20250920202136523

image-20250920202239140

image-20250920202447800

image-20250920202638075

image-20250920202715922

image-20250920202833725

image-20250920202956255

image-20250920203155061

image-20250920203234093

image-20250920203258657

image-20250920203442997

image-20250920203816742

image-20250920203934961

image-20250920204050374

image-20250920204232730

image-20250920204446228

image-20250920204612290

32.函数返回值return

image-20250920204927377

image-20250920205046714

image-20250920205209895

image-20250920210133079

image-20250920210321516

33.函数的作用域

image-20250920210847421

image-20250920211014976

image-20250920211346837

34.变量的访问原则

image-20250920211616674

image-20250920211823615

image-20250920211939779

image-20250920212100253

image-20250920212445865

35.匿名函数

image-20250920212622840

image-20250920212705992

image-20250920212827395

image-20250920212902183

image-20250920213040493

image-20250920213133009

image-20250920213226803

image-20250920213341462

image-20250920213537207

image-20250920213827725

image-20250920213840463

image-20250920213923946

image-20250920214225758

image-20250920214407499

36.逻辑中断

image-20250920214600420

image-20250920214742067

image-20250920215031505

image-20250920215338351

37.特殊值转换为Boolean值

image-20250920215645830

image-20250920215901279

image-20250920220131882

38.对象及其基础使用

image-20250921124304345

image-20250921124401761

image-20250921124548540

image-20250921124626657

image-20250921124811087

image-20250921125048513

image-20250921125207531

image-20250921125556281

image-20250921125645480

image-20250921125914062

image-20250921130109306

image-20250921130210110

image-20250921130244341

image-20250921130824275

image-20250921130720611

image-20250921130908378

39.对象的方法

image-20250921130958848

image-20250921131413753

image-20250921131511175

40.遍历对象

image-20250921131728967

image-20250921132503674

image-20250921132416085

image-20250921132557386

41.内置对象

image-20250921132805771

image-20250921132916961

image-20250921132955300

42.前端信息网站

mdn: Resources for Developers, by Developers

Documenting CSS, HTML, and JavaScript, since 2005.

网址: https://developer.mozilla.org/zh-CN/

image-20250921134115547

43.随机数函数

image-20250921134941252

44.简单和引用数据类型

image-20250921135347522

image-20250921135536787

image-20250921135805356

image-20250921135905804

image-20250921135926127

image-20250921135936360

image-20250921135958979

image-20250921140323121

image-20250921140230181

image-20250921140556208

posted @ 2025-09-21 14:12  Ref-brief  阅读(11)  评论(5)    收藏  举报