JavaScript基础知识 《一》《BOM》

JavaScript

  • JavaScript由三部分构成:ECMAScriptDOMBOM

BOM (浏览器对象模型)

  • bom主要处理浏览器窗口和框架

window

window对象
  • window.document.getElementById("header");
    

    二者相同

  • document.getElementById("header");
    
window 尺寸
  • window.innerHeight - 浏览器窗口的内部高度(包括滚动条)
    
  • window.innerWidth - 浏览器窗口的内部宽度(包括滚动条)
    
  • 示例

     var w=window.innerWidth
     var h=window.innerHeight
    
其他window方法
  • window.open() - 打开新窗口
    window.close() - 关闭当前窗口
    window.moveTo() - 移动当前窗口
    window.resizeTo() - 调整当前窗口的尺寸
    

window location

  • window.location对象用于获取当前页面的URL(全球资源定位器(Uniform Resource Locator)),并把浏览器重新定向到新的页面

  • window.location对象在编写时可以不用window前缀,例如

    • location.hostname 返回 web 主机的域名
      location.pathname 返回当前页面的路径和文件名
      location.port 返回 web 主机的端口 (80 或 443)
      location.protocol 返回所使用的 web 协议(http: 或 https:)
      
location href
  • location.href属性返回当前页面的URL

    • <script>
      document.write(location.href);
      </script>
      

window history

  • window history对象包含浏览器的历史

  • 可不用window前缀

    • history.back() 与在浏览器中点击后退按钮相同
      history.forword  与在浏览器中点击前进按钮相同
      history.go()   可以实现向前后退功能
      
    • <head>
      <script>
      function goBack()
      {
          window.history.back()
      }
      </script>
      </head>
      <body>
       
      <input type="button" value="Back" οnclick="goBack()">
       
      </body>
      
    • <head>
      <meta charset="utf-8">
      <script>
      function goForward()
      {
          window.history.forward()
      }
      </script>
      </head>
      <body>
       
      <input type="button" value="Forward" οnclick="goForward()">
      </body>
      
    • function a(){
          history.go(1);  // go() 里面的参数表示跳转页面的个数 例如 history.go(1) 表示前进一个页面
      }
      
      function b(){
          history.go(-1);  // go() 里面的参数表示跳转页面的个数 例如 history.go(-1) 表示后退一个页面
      }
      
      function a(){
          history.go(0);  // go() 里面的参数为0,表示刷新页面
      }
      

弹窗

  • 在JavaScript中可以创建三种消息框:警告框,确认框,提示框。
警告框
  • window.alert("sometext");
    
  • window.alert() 方法可以不带上window对象,直接使用alert()方法。

  • 示例

     <head>
     <script>
     function myFunction()
     {
         alert("这是一个警告框!");
     }
     </script>
     </head>
     <body>
     
     <input type="button" οnclick="myFunction()" value="显示警告框">
     
     </body>
    
确认框
  • window.confirm("sometext");
    
  • window.confirm(); 方法可以不带上window对象,直接使用confirm()方法。

  • <head>
    		<meta charset="UTF-8">
    		<title></title>
    		<script>
    			var r=confirm("按下按钮");
    if (r==true)
    {
        x="你按下了\"确定\"按钮!";
    }
    else
    {
        x="你按下了\"取消\"按钮!";
    }
    		</script>
    	</head>
    	//提示:确认  取消
    
提示框
  • window.prompt("sometext","defaultvalue");
    
  • window.prompt() 方法可以不带上window对象,直接使用prompt()方法。

    • 	<script>
      	var person=prompt("请输入你的名字","張三");
      if (person!=null && person!="")
      {
        
        
      }	</script>
      

计时事件

  • 设定一个时间间隔后执行代码,由两个关键方法
setlnterval()方法
  • setInterval() 间隔指定的毫秒数不停地执行指定的代码

  • window.setInterval("javascript function",milliseconds);
    
  • 可以不使用window前缀

  • setlnterval()第一个参数是函数(function),第二个参数是间隔的毫秒数

  • 示例

    • var myVar=setInterval(function(){myTimer()},1000);
       
      function myTimer()
      {
          var d=new Date();
          var t=d.toLocaleTimeString();
          document.getElementById("demo").innerHTML=t;
      }
      
  • 停止执行

  • clearInterval() 方法用于停止 setInterval() 方法执行的函数代码。

  • window.clearInterval(intervalVariable)
    
  • 要使用 clearInterval() 方法, 在创建计时方法时必须使用全局变量:

  • myVar=setInterval("javascript function",milliseconds);
    
  • 示例

    • <p id="demo"></p>
      <button οnclick="myStopFunction()">停止</button>
      <script>
      var myVar=setInterval(function(){myTimer()},1000);
      function myTimer(){
          var d=new Date();
          var t=d.toLocaleTimeString();
          document.getElementById("demo").innerHTML=t;
      }
      function myStopFunction(){
          clearInterval(myVar);
      }
      </script>
      
setTimeout()方法
  • 语法

    • myVar= window.setTimeout("javascript function", milliseconds);
      
    • setTimeout() 方法会返回某个值。在上面的语句中,值被储存在名为 myVar 的变量中。如果要取消这个 setTimeout(),可以使用这个变量名来指定它

    • setTimeout() 的第一个参数是含有 JavaScript 语句的字符串。对函数的调用

      第二个参数指示从当前起多少毫秒后执行第一个参数。

  • 等待三秒,跳出弹框

    • setTimeout(function(){alert("Hello")},3000);
      
  • 停止运行

  • clearTimeout() 方法用于停止执行setTimeout()方法的函数代码。

  • 语法

    • window.clearTimeout(timeoutVariable)
      
  • 要使用clearTimeout() 方法, 必须在创建超时方法中(setTimeout)使用全局变量:

    • myVar=setTimeout("javascript function",milliseconds);
      
  • 示例

    • var myVar;
       
      function myFunction()
      {
          myVar=setTimeout(function(){alert("Hello")},3000);
      }
       
      function myStopFunction()
      {
          clearTimeout(myVar);
      }
      

document

document.querySelector()
  • document.querySelector 方法接受一个 CSS 选择器作为参数,返回匹配该选择器的元素节点。如果

    有多个节点满足匹配条件,则返回第一个匹配的节点。如果没有发现匹配的节点,则返回 null 。

document.querySelectorAll(css选择器)
  • 返回一个数组
document.getElementsByTagName()
  • document.getElementsByTagName 方法搜索 HTML 标签名,返回符合条件的元素。它的返回值是一 个类似数组对象( HTMLCollection 实例),可以实时反映 HTML 文档的变化。如果没有任何匹配的元 素,就返回一个空集。
document.getElementsByClassName()
  • 通过class属性查找元素,返回一个数组
document.getElementsByName()
  • document.getElementsByTagName(“某个标签的名”) 如果是用document调用这个方法,则获取的是所有这个标签名的元素,返回一个HTMLCollection对象
document.getElementById()
  • document.getElementById(“某个id名”) 获取该id所对应的标签,如果有多个id,只取第一个这个方法一般只用document调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值