JavaScript—day03
今天主要是学习了一下JavaScript的BOM对象,节点和表单的相关操作,具体如下
1.操作BOM对象
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> //window 代表 浏览器窗口(可以调整浏览器窗口试试....) window.alert(1)//window的弹窗 window.innerHeight//浏览器的内部宽度 window.innerWidth//浏览器的内部长度 window.outerHeight//浏览器的外部宽度 window.outerWidth//浏览器的外部长度 //Navigator,封装了浏览器的信息(大多时候我们不会使用navigator对象,因为会被人为修改) navigator.appName 'Netscape' navigator.appVersion '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36' navigator.userAgent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36' navigator.platform 'Win32' //screen(屏幕尺寸) screen.width 1536 screen.height 864 //location:代表当前页面的URL信息 host: "www.baidu.com"//主机 href: "https://www.baidu.com/"//当前指定的位置 protocol: "https:"//协议 reload: ƒ reload()//重新加载(刷新) //设置新的地址 location.assign('https://www.bilibili.com/') //document:代表当前的页面,html dom文档树 document.title '百度一下,你就知道' document.title='你真帅' '你真帅' //获取cookie document.cookie //服务器端可以设置cookie :httpOnly //history history.back()//后退 history.forward()//前进 </script> </body> </html>
2.Document
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <dl id="app"> <dt>java</dt> <dd>javaSE</dd> <dd>javaEE</dd> </dl> <script> //获取具体的文档树节点 var dl = document.getElementById('app') </script> </body> </html>
3.获得Dom节点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="father"> <h1>标题一</h1> <p id="p1">p1</p> <p class="p2">p2</p> </div> <script> //对应css选择器 var h1 = document.getElementsByTagName('h1'); var p1 = document.getElementById('p1'); var p2 = document.getElementsByClassName('p2'); var father = document.getElementById('father'); var childrens = father.children;//获取父节点下的所有子节点 father.firstChild father.lastChild </script> </body> </html>
4.更新节点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="id1"> </div> <script> /*id1.innerText='456' 修改文本的值 id1.innerHTML='<strong>123</strong>' 可以解析HTML文本标签 */ var id1 = document.getElementById('id1'); id1.innerText = '你真帅!!!' id1.style.color = 'red' id1.style.fontSize = '40px' id1.style.padding = '50px' </script> </body> </html>
5.删除节点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="father"> <h1>标题一</h1> <p id="p1">p1</p> <p class="p2">p2</p> </div> <script> //删除节点的步骤:先获取父节点,再通过父节点删除自己 var self = document.getElementById('p1'); var father = p1.parentElement; father.removeChild(self) //删除是一个动态的过程 father.removeChild(father.children[0]) father.removeChild(father.children[1]) father.removeChild(father.children[2]) //注意:删除多个节点的时候,children是在时刻变化的,删除节点的时候一定要注意 </script> </body> </html>
6.插入节点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p id="js">JavaScript</p> <div id="list"> <p id="se">JavaSE</p> <p id="ee">JavaEE</p> <p id="me">JavaME</p> </div> <script> var js = document.getElementById('js');//已经存在的节点 var list = document.getElementById('list'); list.appendChild(js);//追加到后面 //通过js创建一个新的节点 var newp = document.createElement('p');//创建一个p标签 newp.id = 'newp'; newp.innerText='hello world'; list.appendChild(newp); //把JavaScript插到JavaEE的前面 var ee = document.getElementById('ee'); var js = document.getElementById('js'); var list = document.getElementById('list'); //要包含的节点insertBefore(newNode,targetNode) list.insertBefore(js,ee); </script> </body> </html>
7.操作表单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="post"> <p> <span>用户名:</span> <input type="text" id="username"> </p> <!--多选框的值,就是定义好的value--> <p> <span>性别:</span> <input type="radio" name="sex" value="man" id="boy">男 <input type="radio" name="sex" value="wowen" id="girl">女 </p> </form> <script> var input_text = document.getElementById('username'); var boy_radio = document.getElementById('boy'); var girl_radio = document.getElementById('girl'); input_text.value//得到输入框的值 input_text.value = '123'//修改输入框的值 //对于单选框,多选框等等固定值,boy_radio.value只能取到当前的值 boy_radio.checked;//查看返回的结果,是否为true,如果为true,则被选中 boy_radio.checked = true;//赋值 </script> </body> </html>
8.提交表单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="http://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script> </head> <body> <!--表单绑定提交事件--> <!-- 表单绑定提交事件 onsubmit = 绑定一个提交检测的函数,true,false 将这个结果换回给表单,使用onsubmit接收 --> <form action="http://www.baidu.com/" method="post" onsubmit="return aaa()"> <p> <span>用户名:</span> <input type="text" id="username" name="username"> </p> <p> <span>密码:</span> <input type="password" id="input-password"> </p> <input type="hidden" id="md5-password" name="password"> <!--绑定事件 onclick 被点击--> <button type="submit" >提交</button> </form> <script> function aaa(){ var uname = document.getElementById('username'); var pwd = document.getElementById('password'); var md5pwd = document.getElementById('md5-password'); md5pwd.value = md5(pwd.value); //可以校验判断表单内容,true就是通过提交,false就是阻止提交 return true; } </script> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律