Knowledge points on BOM operation in JS
BOM operation knowledge points in JS
window object
Global variables and global methods are both Return to window
alert-comfirm-prompt
Let the prompt text on alert, confirm and other pop-up boxes realize line wrapping:\n
// confirm() // 点击确定返回true,取消返回false var btn=document.getElementById("btn"); btn.onclick=function(){ // 弹出确认对话框 var result=window.confirm("您确定要删除吗?删除之后该信息\n将不可恢复!"); if(result){ document.getElementById("box").style.display="none"; } } // prompt("text","defaultText") // text:对话框中显示的纯文本 // defaultText:默认的输入文本 // 点击确认返回文本,点击取消返回null var message=prompt("请输入您的星座","天蝎座"); console.log(message);
open-close
If the url parameter in the open method is empty, the new window will also be opened but no document will be displayed
window.onload = function(){ // 打开子窗口,显示 newwindow.html window.open("newwindow.html","newwindow","width=400,height=200, left=0,top=0,toolbar=no,menubar=no,scrollbars=no,location= no,status=no"); var quit = document.getElementById("quit"); // 点击关闭当前窗口 quit.onclick = function(){ window.close("newwindow.html"); } }
Delayed call to setTimeout()
//调用函数 var fnCall=function(){ alert("world"); } setTimeout(fnCall,5000); //调用匿名函数 var timeout1=setTimeout(function(){ alert("hello"); },2000) clearTimeout(timeout1);
Achieve the following requirements:
(1) 3 seconds after clicking the "Delete" button, the text in p on the page disappears
(2) After clicking the "Delete" button Within 3 seconds, if you click the "Cancel Delete" button, the text in p on the page will not be deleted
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>定时器</title> <style type="text/css"> p{width:400px;height:120px;margin-top:50px;border:2px solid gray;padding:10px;} </style> </head> <body> <input type="button" value="删除"> <input type="button" value="取消删除"> <p>点击"删除"按钮后,里面的内容将在3秒钟后消失; <br/><br/>如点击了"删除"后又不想删除内容,请在点击"删除"按钮3秒之内点击"取消删除"按钮即可</p> <script type="text/javascript"> var btn1=document.getElementsByTagName('input')[0]; var btn2=document.getElementsByTagName('input')[1]; var p=document.getElementsByTagName('p')[0]; var timer; btn1.onclick=function(){ timer=setTimeout(function(){ p.innerHTML=''; },3000); } btn2.onclick=function(){ clearTimeout(timer); } </script> </body> </html>
Verification code countdown case:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script> window.onload=function(){ var btn=document.getElementById("btn"); var times=10; var timer=null; btn.onclick=function(){ if(this.getAttribute("clicked")){return false;} var _this=this; timer=setInterval(function(){ times--; if(times<=0){ clearInterval(timer); _this.value="发送验证码"; //_this.disabled=false; _this.removeAttribute("clicked",false); times=10; }else{ _this.value=times+'秒后重试'; //_this.disabled=true; _this.setAttribute("clicked",true); } },1000) } } </script> </head> <body> <p class="box"> <input type="button" value="发送验证码" id="btn"> </p> </body> </html>
Flashing text:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>闪烁的文字</title> <style type="text/css"> p{ width:200px; height:200px; line-height:200px; border:2px solid gray; text-align:center; color:red; } </style> </head> <body> <h3>会闪烁的文字</h3> <p id="text"> </p> <script type="text/javascript"> var text=document.getElementById('text'); var flag=0; setInterval(function(){ if(flag==0){ flag=1; text.innerHTML='☆☆☆今日特卖☆☆☆'; }else if(flag==1){ flag=0; text.innerHTML='★★★今日特卖★★★'; } },500); </script> </body> </html>
location.href returns the complete URL of the current page
location.hash Return to # after Step
console.log(location.href); console.log(location.hash); var btn=document.getElementById("btn"); btn.onclick=function(){ // 可以实现跳转 location.hash="#top"; } // 返回服务器名称和端口号 // 本地不行,要到服务器上 console.log(location.host); // 返回服务器名称 console.log(location.hostname); // 返回URL中的目录和文件名 console.log(location.pathname); // 返回URL中的查询字符串,以?开头 console.log(location.search);
screen object
setTimeout(function(){ // 会在历史记录中生成新纪录 location.href='index6.html'; window.location='index6.html'; // 不会在历史记录中生成新纪录 location.replace("index6.html"); },1000) document.getElementById("reload").onclick=function(){ // 有可能从缓存中加载 location.reload(); // 从服务器重新加载 location.reload(true); }
navigator object
var btn = document.getElementById("btn"); var btn2 = document.getElementById("btn2"); var btn3 = document.getElementById("btn3"); // 点击btn按钮时回到历史记录的上一步,后退 btn.onclick = function() { // 方法一 history.back(); // 方法二 history.go(-1); } // 点击btn2按钮时回到历史记录的下一步,前进 btn2.onclick = function() { // 方法一 history.forward(); // 方法二 history.go(1); } btn3.onclick = function() { // 前进n步 history.go(n); // 后退n步 history.go(-n); }
This article comes from the
js tutorial column, welcome to learn!
The above is the detailed content of Knowledge points on BOM operation in JS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Ele.me is a software that brings together a variety of different delicacies. You can choose and place an order online. The merchant will make it immediately after receiving the order. Users can bind WeChat through the software. If you want to know the specific operation method , remember to check out the PHP Chinese website. Instructions on how to bind WeChat to Ele.me: 1. First open the Ele.me software. After entering the homepage, we click [My] in the lower right corner; 2. Then in the My page, we need to click [Account] in the upper left corner; 3. Then come to the personal information page where we can bind mobile phones, WeChat, Alipay, and Taobao. Here we click [WeChat]; 4. After the final click, select the WeChat account that needs to be bound in the WeChat authorization page and click Just [Allow];
