JavaScript初級(jí)教程(第五課)第2/4頁(yè)
文字域可以鏈接onBlur、onFocus和onChange事件。當(dāng)有人點(diǎn)擊文字域的里邊時(shí)則發(fā)生onFocus事件。而如果點(diǎn)擊文字域的外面或按了tab鍵時(shí)則發(fā)生onblur事件。如果有人改變了文字域內(nèi)的內(nèi)容然后轉(zhuǎn)到文字域外部的區(qū)域時(shí)則發(fā)生onChange事件。
試著做這些事情看下面的文字域會(huì)發(fā)生什么情況。
以下是編制方法:文字域的編碼:
<input type="text" name="first_text" onFocus="writeIt('focus');" onBlur="writeIt('blur');" onChange="writeIt('change');">
每一個(gè)事件處理器調(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預(yù)定義。主體中的第1行
var word_with_return = the_word + "\n";
將一個(gè)變量word_with_return進(jìn)行初始化為函數(shù)處理后的字符串并加上換行符"\n".。注意"\n" 是標(biāo)準(zhǔn)的計(jì)算機(jī)指令。
下一行
window.document.first_form.the_textarea.value += word_with_return;
將文字區(qū)域的值設(shè)置為其原值加新變量。其作用相當(dāng)于
window.document.first_form.the_textarea.value = window.document.first_form.the_textarea.value + word_with_return;
目前我們已經(jīng)學(xué)習(xí)了文字域和文字區(qū)域(值)的屬性,接下來我們學(xué)習(xí)文字域和文字區(qū)域處理所用的方法:blur()、focus()和select()。
下面的鏈接顯示了focus() 和select()如何工作。注意他們工作一次后可能會(huì)終止功能。
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)用任何對(duì)象方法的做法是一樣的:object_name.method(). 該文字域的名稱是window.document.form_name.text_field_name,所以用下列語(yǔ)句就可調(diào)用文字域的focus()方法。
window.document.method_form.method_text.focus();
相關(guān)文章
javascript創(chuàng)建對(duì)象的幾種模式介紹
下面小編就為大家?guī)硪黄猨avascript創(chuàng)建對(duì)象的幾種模式介紹。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05用javascript自動(dòng)顯示最后更新時(shí)間
用javascript自動(dòng)顯示最后更新時(shí)間...2007-03-03在JavaScript中處理數(shù)組之reverse()方法的使用
這篇文章主要介紹了在JavaScript中處理數(shù)組之reverse()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06