onfocus事件,onblur事件;Focus()方法,Blur()方法,onfocusonblur

來源:互聯網
上載者:User

onfocus事件,onblur事件;Focus()方法,Blur()方法,onfocusonblur

<1>

<pre name="code" class="html"><!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>    <title></title>    <script type="text/javascript"></script>    <!--  注意在使用$的時候需要引入Jqery-->    <!-- $("input[name='name']")-->    <script src="Jquery/jquery-1.4.1.min.js" type="text/javascript"></script>    <script type="text/javascript">        function checkValue(obj) {            if (obj.value == "") {                alert("長度不可為空");                obj.value = "長度不可為空";            }        }         function checkGetfocus(obj) {            //將當前控制項的value設為"哥獲得了焦點"            obj.value = "哥獲得了焦點"        }    </script></head><!--當頁面載入完畢,使用者名稱Getfocus文字框獲得焦點--><body onload="$('#Getfocus').focus()">        <!--================================================================================================-->        <!--當使用者點擊“焦點”按鈕的時候id=Getfocus文字框獲得焦點-->        <input type="text" id="Getfocus" />        <!--blur()失去焦點方法-->        <input type="button" value="Getfocus失去焦點" onclick="document.getElementById('Getfocus').blur() " />        <!--focus()獲得焦點方法-->        <input type="button" value="Getfocus獲得焦點" onclick="document.getElementById('Getfocus').focus();" />                <!--================================================================================================-->        <!--onblur失去焦時間點事件-->        <input type="text" name="txt1" onblur="checkValue(this)" />        <input type="text" name="txt2" onblur="checkValue(this)" />        <input type="text" name="txt3" onblur="checkValue(this)" />        <hr />        <!---------------------------------------------------------------------------------------------------->        <!--onfocus獲得焦時間點事件-->        <!--   this就是指當前控制項本身,onfocus這個事件觸發後,這個事件就會調用checkGetfocus函數,在調用這個函數的時候會把當前控制項本身(自己)作為參數(this)傳遞給處理函數checkGetfocus,-->        <input type="text" name="t1" onfocus="checkGetfocus(this)" />        <input type="text" name="t1" onfocus="checkGetfocus(this)" />        <input type="text" name="t1" onfocus="checkGetfocus(this)" />        <!--================================================================================================--></body></html>






onblur、onfocus的用法

onblur是在控制項失去焦點時觸發
onfocus則相反,是在擷取焦點時觸發

例子
www.w3school.com.cn/htmldom/event_onblur.asp
www.w3school.com.cn/htmldom/event_onfocus.asp
 
onFocus與onBlur事件都不好使

給這個元素加個id,
var ele = document.getElementById("id");
ele.onfocus = function(){alert(1)};
ele.onblur = function(){alert(2)};
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.