国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

Javascript事件實(shí)例詳解

2019-11-20 21:46:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
document是位于html標(biāo)簽之上的,可以說(shuō)是權(quán)力最大的。下面的實(shí)例當(dāng)你單擊頁(yè)面上的任何位置都會(huì)彈出“a”,正是運(yùn)用了document的特性。
復(fù)制代碼 代碼如下:

 <script>       
     document.onclick=function(){
         alert('a');
     };
 </script>
 

獲取鼠標(biāo)位置clientX,clientY---注意這里僅僅只是可視區(qū)的鼠標(biāo)位置
復(fù)制代碼 代碼如下:

 <script>
    document.onclick=function(ev){
        if(ev)
        {
            alert(ev.clientX+','+ev.clientY);
        }
        else{
            alert(event.clientX+','+event.clentY);
        };
    };
</script>
 

或者
復(fù)制代碼 代碼如下:

 <script>
    document.onclick=function(ev){
    /*    if(ev)
        {
            alert(ev.clientX+','+ev.clientY);
        }
        else{
            alert(event.clientX+','+event.clentY);
        };
    };*/
    var oEvent=ev||event;
    alert(oEvent.clientX+','+oEvent.clientY);
    };
</script>
 

事件冒泡---一層一層疊加的元素在一起,形成事件冒泡,比如下面的例子:document的最大范圍影響了div的響應(yīng)。
復(fù)制代碼 代碼如下:

 <script>
    window.onload=function(){
        var obtn=document.getElementById('btn1');
        var odiv=document.getElementById('div1');
        obtn.onclick=function(ev){
            var oEvent=ev||event;
            odiv.style.display='block';
            oEvent.cancelBubble=true;//清除冒泡
        };
        document.onclick=function(){
            odiv.style.display='none';
        };
    };
</script>
</head>
<body>
<input type="button" value="顯示" id="btn1"/>
<div id="div1" style="width:100px;height:150px;background:#ccc;"></div>
</body>
 

鼠標(biāo)移動(dòng)---在可視區(qū)有效
復(fù)制代碼 代碼如下:

 <title>鼠標(biāo)移動(dòng)</title>
<script>
    window.onmousemove=function(ev){
        var oEvent=ev||event;
        var odiv=document.getElementById('div1');
        odiv.style.left=oEvent.clientX+'px';
        odiv.style.top=oEvent.clientY+'px';
    };
</script>
</head>
<body>
<div id="div1" style="width:50px;height:50px;background:blue;position:absolute;"></div>
</body>
 

 鍵盤(pán)改變位置和方向---通過(guò)keycode獲取鍵盤(pán)的鍵值來(lái)執(zhí)行相應(yīng)的操作。
復(fù)制代碼 代碼如下:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#div1 {width:100px; height:100px; background:#CCC; position:absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<script>
document.onkeydown=function (ev)
{
    var oEvent=ev||event;
    var oDiv=document.getElementById('div1');

    //←        37
    //右        39

    if(oEvent.keyCode==37)
    {
        oDiv.style.left=oDiv.offsetLeft-10+'px';
    }
    else if(oEvent.keyCode==39)
    {
        oDiv.style.left=oDiv.offsetLeft+10+'px';
    }
};
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
 

鼠標(biāo)跟隨小尾巴
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
div {width:10px; height:10px; background:red; position:absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<script>
window.onload=function ()
{
    var aDiv=document.getElementsByTagName('div');
    var i=0;

    document.onmousemove=function (ev)
    {
        var oEvent=ev||event;

        for(i=aDiv.length-1;i>0;i--)
        {
            aDiv[i].style.left=aDiv[i-1].style.left;
            aDiv[i].style.top=aDiv[i-1].style.top;
        }

        aDiv[0].style.left=oEvent.clientX+'px';
        aDiv[0].style.top=oEvent.clientY+'px';
    };
};
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>

keycode
復(fù)制代碼 代碼如下:

 <script>
document.onkeydown=function (ev)
{
    var oEvent=ev||event;

    alert(oEvent.keyCode);
};
</script>
 

 ctrlKey---可以通過(guò)ctrl+enter組合鍵來(lái)提交內(nèi)容
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<script>
window.onload=function ()
{
    var oBtn=document.getElementById('btn1');
    var oTxt1=document.getElementById('txt1');
    var oTxt2=document.getElementById('txt2');

    oBtn.onclick=function ()
    {
        oTxt1.value+=oTxt2.value+'/n';
        oTxt2.value='';
    };

    oTxt2.onkeydown=function (ev)
    {
        var oEvent=ev||event;

        if(oEvent.ctrlKey && oEvent.keyCode==13)
        {
            oTxt1.value+=oTxt2.value+'/n';
            oTxt2.value='';
        }
    };
};
</script>
</head>
<body>
<textarea id="txt1" rows="10" cols="40"></textarea><br />
<input id="txt2" type="text" />
<input id="btn1" type="button" value="留言" />
</body>
</html>
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 揭西县| 读书| 新沂市| 鹤壁市| 黄陵县| 洱源县| 磐石市| 南安市| 武功县| 晋州市| 绥中县| 胶州市| 清丰县| 贵港市| 揭东县| 繁昌县| 张家口市| 巴青县| 定州市| 泸水县| 陆河县| 嫩江县| 雅安市| 广丰县| 舞阳县| 崇州市| 临沧市| 马龙县| 苍梧县| 大方县| 丹凤县| 灌阳县| 句容市| 百色市| 海城市| 恭城| 全椒县| 泾阳县| 绥德县| 南和县| 绥德县|