Home >Web Front-end >JS Tutorial >How to operate BOM object in js? Methods to operate BOM objects in js
The content of this article is about how to operate BOM objects in js? The method of operating BOM objects in js has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Three core objects:
window:
[window].alert Only prompt message exists alert(message)
[window].prompt allows users to manually enter var obj=prompt(message)
[window].confirm helps users make judgments. When the user clicks OK, it returns true,
to return false
open(url) Open the specified url address
close() Close the current tab
confirm.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>confirm弹层</title> <script> function del(){ var flag = confirm('您确定要删除吗?'); if(flag){ alert("拜拜"); } else{ alert("我们还嫩能够继续。"); } } function toBaidu(){ open('https://www.baidu.com'); } </script></head><body> <!--删除按钮--> <!--<buttun onclick="javascript:confirm('您确定要删除吗?')">删除</buttun>--> <buttun onclick="del()">删除</buttun> <buttun onclick="toBaidu()">百度</buttun> <a href="history.html">去history页面</a> <button onclick="javascript:history.forward()">前进</button> </body> </html>
success.html
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>欢迎页</title> <script> function closeThis(){ close('./success.html'); } </script></head><body> <img src="./img/gaoyuanyuan.png" /> 欢迎您! <button onclick="closeThis()">关闭</button> </body> </html>
setTimeout(function(){},毫秒) 指定时间延迟操作 只操作一次 var i=setInterval(function(){},毫秒) 指定时间延迟操作 操作多次(每隔一段时间操作一次) clearInterval(i) 清空定时特效 onload 事件:等待页面上的所有元素加载完毕
timing.html
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>定时函数</title> <script> function print(){ setTimeout( function(){ alert("我叫jhz"); },5000 ); } print(); </script> </head><body> <input name="btn" type="button" value="定时" onclick="print();" /> </body> </html>
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>定时函数</title> <script> window.onload=function(){ //保证页面上的元素加载完毕后 var btn=document.getElementsByTagName('button'); alert(btn); // 元素中的文本 innerText alert(btn.innerText); } </script> </head><body> <button id="btn">点击获取验证码</button></body></html>
history.html
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>history对象</title></head><body> <!--<button onclick="javascript:history.forward()">前进</button>--> <button onclick="javascript:history.back()">后退</button> </body> </html>
history:window
History:back() Go back ===>go(-1)
forward() forward ===> go(1)
Go (index) can move forward and back.
注 Note: The forward and backward button on the browser is the same effect.
When we access a link from a web page, we can return to the web page, and then on the web page, we can advance again to the link page we just jumped to. (Like a continuous previous step and next step)
location: window
host:主机地址+端口号 hostname:主机名 port:端口号 protocol:协议 href:发送请求到指定URL reload() 刷新当前页面 replace(url) 替换 以新的页面替换当前页面
location.html
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>location</title> <script> function toBaidu(){ //get请求 location.href="http://www.baidu.com"; Math Date } </script></head><body> <button onclick="javascript:alert(location.host)">查看主机地址</button> <button onclick="javascript:alert(location.hostname)">查看主机名</button> <button onclick="javascript:alert(location.port)">查看端口</button> <button onclick="javascript:alert(location.protocol)">查看协议</button> <button onclick="javascript:alert(location.href)">查看href</button> <button onclick="toBaidu()">去百度</button> <button onclick="javascript:location.reload()">刷新</button> <!--get请求--> <button onclick="javascript:location.replace('http://www.baidu.com')">replace百度</button></body></html>
—Document Object XML
document.getXXX()
var obj=document.getElementById("id attribute value") Obtaining the element object based on the id of the page element returns a single node object
————————————All returned are node collections———— ————-
document.getElementsByName("name attribute value") obtains the element object based on the name attribute of the page element
document.getElementsByClassName("class属性值") document.getElementsByTagName("节点名称")
Javascript's built-in object function
**Math对象:** Math.ceil(number)向上取整 Math.floor(number) 向下取整 Math.round(number)四舍五入 Math.random() 生成0~1的随机数 **Date日期对象:** getDate = function() {}; // 获取当前日期(天) } getDay = function() {}; //一周中的第几天 } getMonth = function() {};// 一年中的第几月 } getFullYear = function() {};// 返回年份 4位数 } getHours = function() {}; //一天的第几个小时 } getMilliseconds = function() {}; //获取毫秒 当前分钟 } getMinutes = function() {};//分钟 当前小时 } getSeconds = function() {}; //秒 当前分钟 } getTime = function() {}; //时间 1970年1月1日 到现在的毫秒数
date. html
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>date</title> <script> window.onload=function(){ var date = new Date(); alert(date.getDate());//获取当前日期(天) alert(date.getDay());//一周中的第几天 alert(date.getMonth());//一年中的第几月 0-7(cong0开始) alert(date.getFullYear());// 返回年份 4位数 alert(date.getHours());//一天的第几个小时 alert(date.getMilliseconds());//获取毫秒 当前分钟 alert(date.getMinutes ());//分钟 当前小时 alert(date.getSeconds ());//秒 当前分钟 alert(date.getTime());//时间 1970年1月1日 到现在的毫秒数 document.write("结束。"); } </script></head><body></body></html>
Related recommendations:
Content analysis of date object data in js (with case)
Writing js components What are the things that need to be paid attention to? (Method introduction)
The above is the detailed content of How to operate BOM object in js? Methods to operate BOM objects in js. For more information, please follow other related articles on the PHP Chinese website!