JavaScript DHTML/Document/Focus

Материал из Web эксперт
Перейти к: навигация, поиск

Focus blur

    
<html>
<body>
<script language="JavaScript">
function function1(){
   document.getElementById("myButton").blur();
   document.getElementById("myButton").innerText = "This button lost its focus";
   alert("The button lost its focus"); 
} 
</script>
<input id="myButton" 
       type="button" 
       onclick="function1();" 
       value="Tab to put in focus." onFocus="this.innerText="In focus. Click me to lose focus"">
</body>
</html>



Has Focus ?

    
<html>
<body>
<button onclick="alert(document.hasFocus());">document.hasFocus()</button>
</body>
</html>



Set focus to an element

    
<html>
<body>
<script language="JavaScript">
    function function1() {
        document.all.myElement.focus();
    }
    function function2() {
        alert("onFocus event has been fired");
    }
</script>
<input id="myElement" type="text" value="" onfocus="function2();">
<input type="button" 
       value="Give focus to the text input element and fire the event" 
       onclick="function1();">
</body>
</html>