JavaScript初級教程(第五課)第2/4頁
文字域可以鏈接onBlur、onFocus和onChange事件。當有人點擊文字域的里邊時則發(fā)生onFocus事件。而如果點擊文字域的外面或按了tab鍵時則發(fā)生onblur事件。如果有人改變了文字域內(nèi)的內(nèi)容然后轉到文字域外部的區(qū)域時則發(fā)生onChange事件。
試著做這些事情看下面的文字域會發(fā)生什么情況。
以下是編制方法:文字域的編碼:
<input type="text" name="first_text" onFocus="writeIt('focus');" onBlur="writeIt('blur');" onChange="writeIt('change');">
每一個事件處理器調(diào)用函數(shù)writeIt(),該函數(shù)已作了定義。編碼如下:
<script language="JavaScript">
<!-- hide me
function writeIt(the_word)
{
var word_with_return = the_word + "\n";
window.document.first_form.the_textarea.value += word_with_return;
}
// show me -->
</script>
前幾行是典型的JavaScript預定義。主體中的第1行
var word_with_return = the_word + "\n";
將一個變量word_with_return進行初始化為函數(shù)處理后的字符串并加上換行符"\n".。注意"\n" 是標準的計算機指令。
下一行
window.document.first_form.the_textarea.value += word_with_return;
將文字區(qū)域的值設置為其原值加新變量。其作用相當于
window.document.first_form.the_textarea.value = window.document.first_form.the_textarea.value + word_with_return;
目前我們已經(jīng)學習了文字域和文字區(qū)域(值)的屬性,接下來我們學習文字域和文字區(qū)域處理所用的方法:blur()、focus()和select()。
下面的鏈接顯示了focus() 和select()如何工作。注意他們工作一次后可能會終止功能。
Mouseover to focus | Mouseover to select |
以下為表單和鏈接的編碼:
<form name="method_form">
<input type="text" name="method_text" size=40 value="Hey, hey, we're the monkeys">
</form>
<a href="#" onMouseOver="window.document.method_form.method_text.focus();">Mouseover to focus</a>
<a href="#" onMouseOver="window.document.method_form.method_text.select();">Mouseover to select</a>
其使用方法和調(diào)用任何對象方法的做法是一樣的:object_name.method(). 該文字域的名稱是window.document.form_name.text_field_name,所以用下列語句就可調(diào)用文字域的focus()方法。
window.document.method_form.method_text.focus();
相關文章
在JavaScript中處理數(shù)組之reverse()方法的使用
這篇文章主要介紹了在JavaScript中處理數(shù)組之reverse()方法的使用,是JS入門學習中的基礎知識,需要的朋友可以參考下2015-06-06