JS 的DOM操作

DOM的概念:文档对象模型 --树模型
文档:标签文档,对象:文档中每个元素对象,模型:抽象化的东西
!windows 对象:浏览器窗口信息
!document对象:浏览器显示的页面文件

!都属于DOW操作

onclick 单击

<body>
        <div style="200px;height:200px; background-color:#0F0" onclick="p()">
        </div>
    </body>
</html>
    <script type="text/javascript">
    function p()
    {
     alert("pp")   
    }
    </script>

ondblclick 双击

<body>
        <div style="200px;height:200px; background-color:#0F0" ondblclick="p()">
        </div>
    </body>
</html>
    <script type="text/javascript">
        function p()
        {
         alert("pp")   
        }

  onkeydown 按键摁下时; 
onkeyup 按键松开时; 
onkeypress 按下就触发; 
onmousedown 鼠标摁下; 
onmousemove 鼠标移动;
onmouseout 鼠标移出;

<body>
        <div style="200px;height:200px; background-color:#0F0" onmouseout="p()">
        </div>
    </body>
</html>
    <script type="text/javascript">
        function p()
        {
         alert("pp")   
        }
    </script>

  onmouseover 鼠标移上;

<body>
        <div style="500px;height:500px; background-color:#0F0" onmouseover="S()">
        </div>
    </body>
</html>
    <script type="text/javascript">
        function S()
        {
         alert("试试")   
        }
    </script>

  表单中的:
onblur 失去焦点触发;(文本框中有个鼠标输入样式在闪烁,失去焦点就是文本框中没有鼠标闪烁)

 <body>
        <input type="text" value="鼠标闪不闪" onblur="p()"/>
    </body>
</html>
    <script type="text/javascript">
        function p()
        {
         alert("pp")   
        }
    </script>

 

onfocus 获得焦点;

<body>
        <input type="text" value="鼠标闪不闪" onfocus="p()"/>
    </body>
</html>
    <script type="text/javascript">
        function p()
        {
         alert("pp")   
        }
    </script>

onchange 改变时触发;(文本框中内容改变触发)

<body>
        <input type="text" value="鼠标闪不闪" onchange="S()"/>
    </body>
</html>
    <script type="text/javascript">
        function S()
        {
         alert("试试")   
        }
    </script>

onselect 选中时触发;文本框中内容选中时触发

<body>
        <input type="text" value="鼠标闪不闪" onselect="o()"/>
    </body>
</html>
    <script type="text/javascript">
        function o()
        {
         alert("pp")   
        }
    </script>

1.window.open("第一部分","第二部分","第三部分","第四部分");
特征参数:
第一部分:写要打开的页面地址
第二部分:打开的方式,_blank 是在新窗口打开 _self
第三部分:控制打开的窗口,可以写多个,用空格隔开
              toolbar=no新打开的窗口无工具条 
              menubar=no无菜单栏 status=no无状态栏 
              width=100 height=100 宽度高度
              left=100 打开的窗口距离左边多少距离
              resizable=no窗口大小不可调 
              scrollbars=yes 出现滚动条
              location=yes 有地址栏

<body>
        <input type="button" value="随便" onclick="S()"/>
    </body>
</html>
    <script type="text/javascript">
        function S()
        {
         window.open("www.html","_blank","width=300 height=300");
        }
    </script>

返回值:新打开的窗口对象

2:最简单的打开窗口
window.open("http://www.baidu.com","_blank","toolbar=no");  在一个新窗口中打开百度。

3:打开窗口,并保存在一个变量中
var w= window.open();

4:只打开窗口一次,例子如下:

if(w==null)
{
w=window.open("http://www.baidu.com","_blank","toolbar=no");
}

这里用一个if语句,判断w的值是否为空,打开一个窗口之后w的值就不为空了,之后再点击鼠标调用此函数则不执行打开新窗口。

 close():关闭指定的窗口  

    window.close():关闭当前窗口
     w.close():关闭w窗口
     关闭多个子窗口:放在数组中的窗口:w[i].close();
     关闭打开当前窗口的源窗口
     window.opener.close();

<body>
     
        <input type="button"  value="打开窗口" onclick="Y()"/>
        <input type="button"  value="关闭窗口" onclick="G()"/>
</body>
    <script type="text/javascript">
       var w = window.open();
        function Y()
        {
            if(w==null)
            {
                w = window.open("Untitled-2.html","_blank","width=500 height=500");
            }
        }
         
        function G()
        {
            w.close();
        }
 
    </script>

间隔和延迟

window.setInterval("要执行的代码",间隔的毫秒数) (每隔多少毫秒,后一直出)

<body>
     
        <input type="button"  value="打开窗口" onclick="Y()"/>
        <input type="button"  value="关闭窗口" onclick="G()"/>
</body>
    <script type="text/javascript">
       var w = window.open();
        function Y()
        {
            if(w==null)
            {
                w = window.open("Untitled-2.html","_blank","width=500 height=500");
            }
        }
         
        function G()
        {
            w.close();
        }
        function J ()
        {
            alert("aa")
        }
        window.setInterval("alert('aa')","1000")
    </script>
 

window.clearInterval(间隔的id); 循环一次之后用来清除隔几秒执行的代码

window.setTimeout("要执行的代码",延迟的毫秒数) 
window.clearTimeout(延迟的id);清除setTimeout,一般延迟执行较为常用。(只出一次)

<body>

     
        <input type="button"  value="打开窗口" onclick="Y()"/>
        <input type="button"  value="关闭窗口" onclick="G()"/>
</body>
    <script type="text/javascript">
       var w = window.open();
        function Y()
        {
            if(w==null)
            {
                w = window.open("Untitled-2.html","_blank","width=500 height=500");
            }
        }
         
        function G()
        {
            w.close();
        }
        function J ()
        {
            alert("aa")
        }
        window.setTimeout("J()",2000)
    </script>
原文地址:https://www.cnblogs.com/xiaohaihuaihuai/p/7732899.html