【JavaScript】JavaScript基础-浏览器对象

小破孩
2022-06-21 / 0 评论 / 129 阅读 / 正在检测是否收录...
弹出框

    alert(1);
    var   ret   =   prompt("请输入内容!","");
    var v = confirm('此操作不可恢复,确认吗?');  返回true可操作
    window.close();关闭当前窗口
    window.print();打印
计时器

    function fn() {
        var d=new Date();
        var t=d.toLocaleTimeString();
        document.getElementById("a1").innerHTML=t;
    }
     
    var t = setTimeout("fn()",5000);    执行一次
    clearTimeout(t);    提前结束
    
    var id=setInterval("fn()",1000);//每1秒执行一次
    clearInterval(id); 取消定时
读写localStorage,长时间存储 sessionStorage关闭后删除

    localStorage.setItem("key1", "123");
    localStorage.removeItem("bar");
    var foo = localStorage.getItem("bar");

选择器

    var id=document.getElementById("id");
    var id = document.querySelector("#k");
    支持标签,.class,a[target=_blank]属性
    var id = document.querySelectorAll("li");

window

    document.write("123") 输出
    console.log("控制台")
     
    var url = location.href;
    location.href='http://www.baidu.com';
    
    var domain = document.domain;//不含http
    
    var url = (window.location.href).substring(0,(window.location.href).indexOf(document.location.pathname));//包含http
    
    
    var from = document.referrer;来源
    
    location.reload(true);刷新
    history.go(0);
    
    history.go(1);前进
    history.go(-1);后退
    
    document.title;
    document.title = "标题";
    
    document.querySelector('#c').dataset.sex = "男";data属性
    document.querySelector('#c').dataset.sex;
    
    document.getElementById('demo1').setAttribute('role', 'button'); 设置获取属性
    document.getElementById('foo').removeAttribute('role');
    
    document.getElementById('foo').innerHTML = 'Goodbye!';设置值

逻辑

    if (time<10)
    {
        document.write("<b>早上好</b>");
    }
    else if (time>=10 && time<16)
    {
        document.write("<b>今天好</b>");
    }
    else
    {
        document.write("<b>晚上好!</b>");
    }
    
    switch(n)
    {
        case 1:
            执行代码块 1
            break;
        case 2:
            执行代码块 2
            break;
        default:
            与 case 1 和 case 2 不同时执行的代码
    }
    
    for (var i=0;i<cars.length;i++)
    { 
        document.write(cars[i] + "<br>");
    }
    
    while (条件)
    {
        需要执行的代码
    }

监听事件

     var el=  document.querySelector('#btn');
     el.addEventListener('click',function(){
         document.getElementById('loading1').classList.add('loading1');
     })
0

评论 (0)

取消