IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> Javascript 全英入门<二> -> 正文阅读

[JavaScript知识库]Javascript 全英入门<二>

DOM

DOM: Document Object Model 文档对象模型

getElementByld()

getElementsByTagName()

var pp=document.getElementsByTagName('p');  
  for(var i=1;i<pp.length;i++){
    pp[i].style.color="red";
    alert(`showing the paragraph ${i}`);
    //to see the loop is running or it runs so fast
   } ;

getElementsByClassName()

<p id="one"> let's change the world</p>
    <p> or maybe just be yourself</p>
    <p class="two">or just let it go</p>
    <p class="two">but you should listen to your heart</p>
    <script>
    var pp=document.getElementsByClassName('two');  
      for(var i=0;i<pp.length;i++){
          pp[i].style.color="red";
      } ;
    </script>

querySelectorAll()

<div id="one"> 
    <p> or maybe just be yourself</p>
    <p class="two">or just let it go</p>
    <p class="two">but you should listen to your heart</p>
    </div>
    <script>
    var pp=document.querySelectorAll('.two');  
    for(var i=0;i<pp.length;i++){
           pp[i].style.color="red";}
    </script>

innerHTML

<div id="one"> 
    <p> or maybe just be yourself</p>
    <p >or just let it go</p>
    <p >but you should listen to your heart</p>
    </div>
    <script>
     var mydiv = document.getElementById('one');
     mydiv.innerHTML ="<p>oh who is drunken</p>";
     //document.getElementById('one').innerHTML ="<p>oh who is drunken</p>";
    </script>

classname

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>freyua's page</title>
    <style>
        .blue{color:blue;}
    </style>
</head>
<body>
    <h1>Loops</h1>
    <div id="one"> 
    <p> or maybe just be yourself</p>
    <p>or just let it go</p>
    <p>but you should listen to your heart</p>
    </div>
    <script>
     document.querySelector('p').className="blue";
     
    </script>
</body>
</html>   

appendchild套圈

 <script>
     var mytag =document.createElement('p');
     var mytext = document.createTextNode("here is a new story");
     mytag.appendChild(mytext);
     var mydiv=document.querySelector('div');
     mydiv.appendChild(mytag);   
    </script>

Event

Capturing the events

<button>don't click on </button>
    </div>
    <script>
     var bach =document.querySelector('button');
     function onch(){
         alert("I told you not click on ");
     }
     bach.onclick=onch;  //no parentheses for the function,just reference the name ,like the followed
     // bach.οnclick=function(){alert("you didn't!")}
    </script>

onclick是捕获方法产生的方式

addEventListener同样针对所有产生方法的方式

 bach.addEventListener('click',function(){alert("you didn't listen!")});

addEventListener 【方法】与 event property 【属性】区别:

  • An element can listen for more than one event when attached via the addEventListener method, but can only have one event property on it at a time.
    事件属性一次只有一个元素属性例如onclick 但 addEventListener可以面向所有元素

  • The event listener can be removed via the removeEventListener method, whereas the event property can only be removed by replacing that event property with a different one.

  • onsubmit is an event property, and the addEventListener is a method.

the event object

 bach.addEventListener('click',function(){
        event.target.style.backgroundColor="yellow"; 
        alert("you didn't listen!");});

the event could be change by the parentheses of the function, like the followed: <lolwow
it represents the event object which has methods in JavaScript that enable certain functionality.

 bach.addEventListener('click',function(lolwow){
        lolwow.target.style.backgroundColor="yellow"; 
        alert("you didn't listen!");});
prevent default behaviours
 <a href="https://www.baidu.com">check in baidu</a>
    </div>
    <script>
     var bach =document.querySelector('a');
     bach.addEventListener('click',function(){
        event.preventDefault(); 
        alert("you didn't listen!");});
    </script>

when use the preventDefault the name won’t show on the address bar

    <form method = "get">
     <label>your name:<input type="text" name="name"></label>
       <input type="submit">
    </form>
    <script>
var bach =document.querySelector('form');
bach.addEventListener('submit',function(event){
    event.preventDefault();
    var formdata =document.querySelector('input').value;
    alert(formdata);
});
    </script>

mouseover & mouseout

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        div{
            height:80px;
            width:50px;
            background-color: aquamarine;
        }
    </style>
</head>
<body>
    <h1>Events</h1>
    <div></div>
    <script>
var heading=document.querySelector('h1');
var Divv=document.querySelector('div');
Divv.addEventListener('mouseover',function(){
    heading.innerHTML="the mouse is over the box";
});
Divv.addEventListener('mouseout',function(){
    heading.innerHTML="the mouse has left the box";
});
heading.addEventListener('mouseover',function(){
    heading.innerHTML="the mouse is over the heading lol";
});

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

scroll

change the script part like the followed , you can see it fire when you scroll a little bit. SO console in here is expensive which your page will be bogged down .

    <script>
var pagetop;
window.addEventListener('scroll',function(){
    pagetop =window.pageYOffset;
    //if you define var pagetop in here , it will be more expensive
    console.log(pagetop);
});
    </script>
resize
<script>
window.addEventListener('resize',function(){
    console.log(`the window width is ${window.innerWidth}`);
    console.log(`the window height is ${window.innerHeight}`);
});
    </script>
keydown
    <script>
document.addEventListener('keydown',function(){
   alert("a key was pressed");
   //alert(`${event.which} key was pressed`);
   //alert(`${event.key} key was pressed`);
});
    </script>
  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-27 11:46:11  更:2021-08-27 11:47:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/27 4:47:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计