- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <style>
- div{
- width: 200px;
- height: 200px;
- border:1px solid red;
- /*hidden超出隐藏
- visible超出显示(默认)
- scroll超出滚动显示*/
- overflow: scroll;
- }
- </style>
- </head>
- <body>
- <div>
- <img src="../b.jpg" alt="">
- </div>
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- <style>
- img{
- width: 50px;
- /*垂直对齐方式:
- baseline基线对齐
- top上对齐
- middle中间对齐
- bottom下对齐
- */
- vertical-align: bottom;
- }
- #d1{
- width: 50px;height: 100px;
- background-color: red;
- position: absolute;
- /*设置显示层级, 值越大显示越靠前*/
- z-index: 1;
- }
- #d2{width: 100px;height: 50px;
- background-color: green;
- position: absolute;
- z-index: 2;
- }
- </style>
- <!---->
- </head>
- <body>
- <div id="d1"></div>
- <div id="d2"></div>
- <input type="text"><img src="../b.jpg" alt="">
- </body>
- </html>
变量,数据类型,运算符,各种语句,方法,面向对象
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
-
-
- </head>
- <body>
-
- <!--onclick点击事件属性 alert()弹出提示框-->
- <input type="button" value="内联按钮" onclick="alert('按钮点击了')">
-
- <script>
- //单行注释
- /*多行注释*/
- //在浏览器的控制台输出内容
- console.log("内部JS代码执行")
- </script>
- <!--引入外部的js文件,引入外部文件后 标签体内不能再写js代码-->
- <script src="my.js"></script>
- </body>
-
- </html>
console.log("外部js代码执行")
- for(let i=0;i<10;i++){
-
- let j=i+1;
-
- }
-
- let x = j+i; //不报错 但是访问不到j和i两个变量
- for(var i=0;i<10;i++){
-
- var j=i+1;
-
- }
var x = j+i; //不报错,而且可以访问到j和i的值
"666"==666 true "666"===666 false
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <input type="button" value="按钮" onclick="f1()">
- <script>
- //无参无返回值方法
- function f1() {
- console.log("f1");
- }
- //调用方法
- f1();
- //有参无返回值
- function f2(name, age) {
- console.log(name+":"+age);
- }
- f2("张飞",18);
- //无参有返回值
- function f3() {
- return "这是返回值";
- }
- let info = f3();
- console.log(info);
- //有参有返回值
- function f4(x,y) {
- return x*y;
- }
- let result = f4(5,9);
- console.log(result);
- //第二种声明方法的方式
- let f5 = function (name,age) {
- console.log(name+":"+age);
- }
- //调用
- f5("诸葛亮",50);
- //第三种声明方法的方式
- let f6 = new Function("name","age","console.log(name+':'+age)")
- f6("程咬金",30);
-
-
-
- </script>
- </body>
- </html>
通过选择器选择页面中的元素对象
let 元素对象 = document.querySelector("选择器");
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <div>这是div</div>
- <p id="p1">这是p1</p>
- <p id="p2">这是p2</p>
- <input type="text">
- <input type="button" value="按钮" onclick="f()">
- <script>
- function f() {
- //得到div
- let d = document.querySelector("div");
- //得到文本框
- let i = document.querySelector("input");
- //把文本框里面的内容取出赋值给div
- d.innerText = i.value;
-
- }
- </script>
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <input type="text">
- <input type="button" value="平方" onclick="f()">
- <div></div>
- <script>
- function f() {
- let i = document.querySelector("input");
- let d = document.querySelector("div");
- if (isNaN(i.value)){//判断如果输入的不是数字则进行提示
- d.innerText="输入错误!";
- return;//结束方法
- }
- //字符串进行-*/运算时会自动转成数值
- // d.innerText = i.value*i.value;
- d.innerText = i.value*1+i.value*1;
- }
- </script>
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <input type="text" id="i1">
- <input type="text" id="i2">
- <input type="button" value="加" onclick="f(1)">
- <input type="button" value="减" onclick="f(2)">
- <input type="button" value="乘" onclick="f(3)">
- <input type="button" value="除" onclick="f(4)">
- <div></div>
- <script>
- function f(x) {
- let i1 = document.querySelector("#i1");
- let i2 = document.querySelector("#i2");
- let d = document.querySelector("div");
- if (isNaN(i1.value)||isNaN(i2.value)){
- d.innerText = "输入错误!";
- return;
- }
- switch (x) {
- case 1://加
- d.innerText = i1.value*1+i2.value*1;
- break;
- case 2://减
- d.innerText = i1.value-i2.value;
- break;
- case 3://乘
- d.innerText = i1.value*i2.value;
- break;
- case 4://除
- d.innerText = i1.value/i2.value;
- break;
- }
-
- }
-
- function f1() {
- let i1 = document.querySelector("#i1");
- let i2 = document.querySelector("#i2");
- let d = document.querySelector("div");
- d.innerText = i1.value*1+i2.value*1;
- }
- function f2() {
- let i1 = document.querySelector("#i1");
- let i2 = document.querySelector("#i2");
- let d = document.querySelector("div");
- d.innerText = i1.value-i2.value;
- }
- function f3() {
- let i1 = document.querySelector("#i1");
- let i2 = document.querySelector("#i2");
- let d = document.querySelector("div");
- d.innerText = i1.value*i2.value;
- }
- function f4() {
- let i1 = document.querySelector("#i1");
- let i2 = document.querySelector("#i2");
- let d = document.querySelector("div");
- d.innerText = i1.value/i2.value;
- }
- </script>
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <input type="text" placeholder="请输入1-100的数字">
- <input type="button" value="猜一猜" onclick="f()">
- <div></div>
- <script>
- //得到1-100之间的随机整数
- let x = parseInt(Math.random()*100)+1;
- let count = 0;
- function f() {
- count++;
- let i = document.querySelector("input");
- let d = document.querySelector("div");
- if (i.value>x){
- d.innerText = "猜大了!";
- }else if(i.value<x){
- d.innerText = "猜小了!";
- }else{
- d.innerText = "恭喜你第"+count+"次猜对了!";
- }
- }
-
- </script>
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <h1>0</h1>
- <script>
- //开启只执行一次的定时器
- setTimeout(function () {
- console.log("时间到");
- },3000);
-
- let count=0;
- let h1 = document.querySelector("h1");
- //开启每秒执行一次的定时器
- let timer = setInterval(f,1000);
- function f() {
- count++;
- h1.innerText = count;
- if (count==5){
- //停止定时器
- clearInterval(timer);
- }
- }
- </script>
- </body>
- </html>