首页 > 编程语言 >前端学习-JavaScrip学习-js基础01

前端学习-JavaScrip学习-js基础01

时间:2023-11-04 22:01:30浏览次数:44  
标签:01 console log JavaScrip js let input prompt JS

学习教程:黑马程序员视频链接

JS简介

JS组成

JavaScrip = ECMAscript(语言基础)+web API
web API=BOM+DOM

资料查询网站:MDN

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

    <style>
        .ayu {
            background-color: purple;
        }
    </style>
 </head>
 <body>
    <button class="ayu">button1</button>
    <button>button2</button>
    <button>button3</button>
    <button>button4</button>

    <script>
        let bts = document.querySelectorAll('button');
        for(let i = 0;i < bts.length;i++) {
            bts[i].addEventListener('click',function () {
                document.querySelector('.ayu').className = '';
                this.className = 'ayu';
            })
        }
    </script>
 </body>
 </html>

JS书写位置

1、内部JS
书写位置:页面底部,前(因为浏览器按顺序加载HTML元素,不写在最后可能因为元素还没加载而没有实现对应效果
2、外部JS
通过.js文件以及以下语句实现

<script src="./js/01-js.js"></script>

3、内联JS

<button onclick="alert('this is js')"></button>

数据类型

基本数据类型

number,string,boolean,null,undefined

number

数据类型练习01

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const PI = 3.14;
        let radius = prompt('please input a number');
        document.write(PI*radius*radius);
    </script>
</body>
</html>
string

字符串类型需要用引号包裹

''  --单引号
""  --双引号
``  --反引号

模板字符串-使用反引号+${}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let age = prompt('your age:');
        let uname = prompt('your name:');
        document.write(`your name is ${uname},and your age is ${age} `);
    </script>
</body>
</html>

字面量

true,false 是常量,非字面量
快速理解JavaScript中字面量、变量与常量

引用数据类型

object

检测数据类型

typeof x  //建议用这种
typeof(x)

类型转换

隐式转换
console.log(1 + '1'); \\输出11
console.log(+'123'); \\可将字符串转为数字型
显示转换
Number('123')
parseFloat('12.34ppp')
parseInt('12.34ppp')

类型转换练习01

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        console.log(parseFloat('12.34ppp'));
        console.log(parseFloat('12ppp'));
        console.log(parseFloat('aaa12.34ppp'));
        
        console.log(parseInt('12.34ppp'));
        console.log(parseInt('12ppp'));
        console.log(parseInt('aaa12.34ppp'));

        let num1 = prompt('please input num1:');
        let num2 = +prompt('please input num2:');
        document.write(`the sum of two nums is ${Number(num1) + num2}`);
    </script>
</body>
</html>

案例练习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
    <style>
        h2 {
            text-align: center;
        }

        /* 合并边框 */
        table {
            border-collapse: collapse;
            height: 80px;
            text-align: center;
        }

        th {
            padding: 6px 30px;
        }

        table,
        th,
        td {
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <h2>订单确认</h2>


    <script>
        let price = +prompt("please input the price:");
        let num = +prompt("please input the num:");
        let totalPrice = price*num;
        let addr = prompt("please input the address:"); 

        document.write(`
            <table>
                <tr>
                    <th>商品名称</th>
                    <th>商品价格</th>
                    <th>商品数量</th>
                    <th>总价</th>
                    <th>地址</th>
                </tr>
                <tr>
                    <td>小米手机</td>
                    <td>${price}</td>
                    <td>${num}</td>
                    <td>${totalPrice}</td>
                    <td>${addr}</td>
                </tr>
            </table>
        `);
    </script>
</body>
</html>

标签:01,console,log,JavaScrip,js,let,input,prompt,JS
From: https://www.cnblogs.com/ayubene/p/17804578.html

相关文章

  • JavaScript复习——03 函数
    函数在JS中也是一个对象,它具有其它对象的所有功能,函数中可以存储代码,且可以在需要的时候调用这些代码函数的操作函数的定义函数声明function函数名([参数列表]){ //函数体 return返回值;}函数表达式const函数名=function([参数列表]){ return返回值;}......
  • 【工作01】某科研院所 - 3500每月 - offer - 拒
    经历  导师推荐的一家单位。  跟我说的好听。说什么就开始的半年(6个月实习期)难捱,之后每个月工资(底薪+绩效)比学校发给他的还高。  这里需要先说明一下我所在大学的教师工资。经历过一次工资发放改革,现在绩效平摊到了每一个月里,他一个月的工资到手大概是9000左右,五险一金......
  • B3610 [图论与代数结构 801] 无向图的块 题解
    题目传送门前言本题解内容均摘自我的Tarjan学习笔记。解法Tarjan与无向图无向图与割点(割顶)在一个无向图中,不存在横叉边(因为边是双向的)。一个无向图中,可能不止存在一个割点。割点(割顶):在一个无向图中,若删除节点\(x\)以及所有与\(x\)相关联的边之后,图将会被分......
  • # 学期2023-2024-1 20231401 《计算机基础与程序设计》第六周学习总结
    学期2023-2024-120231401《计算机基础与程序设计》第六周学习总结作业信息这个作业属于哪个课程2023-2024-1-计算机基础与程序设计这个作业要求在哪里2023-2024-1计算机基础与程序设计第六周作业这个作业的目标自学教材:计算机科学概论第7章并完成云班课测试《......
  • Atcoder Grand Contest 016
    给我贺完了?A-Shrinking给定一个串\(s\),每次可以进行如下操作:记串长为\(n\).构造长为\(n-1\)的串\(s'\),满足\(s'_i\)为\(s_i\)或\(s_{i+1}\),令\(s\leftarrows'\).问使\(s\)中所有字符相同的最小操作次数。\(|s|\le100\).按照题意模拟即可,时间复杂度......
  • 面试必刷TOP101:20、数组中的逆序对
    题目题解解法一:暴力法importjava.util.*;publicclassSolution{/***代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可***@paramnumsint整型一维数组*@returnint整型*/publicintInversePairs(in......
  • P2370 yyy2015c01 的 U 盘
    P2370yyy2015c01的U盘基础思路看到题目要求最小需要的最大接口。自然认为既然答案要求接口,那状态方程的值就是接口。一开始状态方程F[i][j],\(i\)为前\(i\)个接口,\(j\)为当前体积。而F[i][j]则为当前最小的最大接口值状态转移方程F[i][j]=min(F[i][j],(F[i-1][j-v[......
  • Day01
    Markdown学习标题:二级标题三级标题字体hello,word!hello,word!hello,word!hello,word!hello,word!  引用选择狂神java,走向人生巅峰 分割线图片 超链接 点击跳转狂神博客  列表AB ABC 表格   名字性别生日......
  • js tab跟随滑动切换tab
    js:letanchors=document.querySelectorAll("div[data-anchor-index]");letobserver=newIntersectionObserver((entries)=>{entries.forEach((entry)=>{if(entry.isIntersecting){letseq=Number(entry.target.ge......
  • 01_Vue 基本语法
    [tocc]Vue.js基本语法一、Vue基础Vue官网英文官网:https://vuejs.org/中文官网:https://cn.vuejs.orgVue概述Vue(读音/vjuː/,类似于view)是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue被设计为可以自底向上逐层应用。Vue的核心库只关注视图......