HTML DOM defaultValue 屬性


定義和用法

defaultValue 屬性設置或返回文本框的初始內容。

注釋:文本框的初始值是位於 <textarea> 和 </textarea> 標簽之間的文本。在表單被重置時,文本框將被恢復為整個值。改變這個屬性的值會改變文本框中當前顯示的文本。

語法:textareaObject.defaultValue=text

實例1:

下面的例子提示出了文本框的默認值:

<html>
<head>
<script type="text/javascript">
function alertDefaultValue()
  {
  alert(document.getElementById('txt1').defaultValue);
  }
</script>
</head>
<body>

<textarea id="txt1">
Hello world....This is a text area
</textarea>
<br />
<input type="button" onclick="alertDefaultValue()" 
value="Alert default value" />

</body>
</html>

實例2:輸入框提示文字灰色效果

<!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" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>輸入框提示文字灰色效果</title>
<style type="text/css">

</style>
<script type="text/javascript">
window.onload=function(){
var oInput=document.getElementById('input1');
oInput.onfocus=function(){
if(oInput.value==oInput.defaultValue){
oInput.value='';
this.style.color='#000';
};
};
oInput.onblur=function(){
if(!oInput.value){
oInput.value=oInput.defaultValue;

this.style.color='#999';
};
};
};
</script>
</head>
<body>
<a href="###">輸入框提示文字灰色效果</a>,
<input type="text" id="input1" name="lee" value="請輸入關鍵詞" style="color:#999999">

</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM