JS 事件控制

onfocus 事件在对象获得焦点时发生。

实例:

<html>
<head>
<script type="text/javascript">
    function setStyle(x)
    {
        document.getElementById(x).style.background="yellow"
    }
</script>
</head>
<body>
    First name: <input type="text" onfocus="setStyle(this.id)" id="fname" /><br />
    Last name: <input type="text" onfocus="setStyle(this.id)" id="lname" />
</body>
</html>

结果:当用鼠标点击input窗口的时候 input背景颜色为黄色

 

onblur 事件会在对象失去焦点时发生;

实例:

<html>
<head>
<script type="text/javascript">
    function upperCase()
    {
        var x=document.getElementById("fname").value
        document.getElementById("fname").value=x.toUpperCase()    //toUpperCase() 方法用于把字符串转换为大写
    } 
</script>
</head>
<body> 
    输入您的姓名: <input type="text" id="fname" onblur="upperCase()"/> 
</body>
</html>

结果:在input窗口中输入小写的英文 当input失去焦点的时候 将刚输入的input的value内容转成大写

 

onload 事件会在页面或图像加载完成后立即发生。

window.onload 回调函数其实是在页面加载完成后(包括图片内容的显示)才会执行,并不是页面加载的等待过程中就执行。

例:

<html>
<head>
<script type="text/javascript">
function load()
{
window.status="Page is loaded"
}
</script>
</head>

<body 
οnlοad="load()"
>
</body>

</html>
onload,加载图片,加载整个body中的内容,所以加在body上

 

posted @ 2020-04-03 15:47  Li&Fan  阅读(239)  评论(0编辑  收藏  举报