2011年11月20日 星期日

常用JavaScript語法(一)

取得或設定物件屬性值

getElementById=由識別元(ById)取得(get)元件(Element)
包含的HTML內容
document.getElementById('ID').innerHTML

使用樣式

document.getElementById('ID').className

顏色

document.getElementById('ID').style.color

背景色

document.getElementById('ID').style.backgroundColor

背景影像

document.getElementById('ID').style.backgroundImage

顯示?

document.getElementById('ID').style.visibility


取得或設定表單物件值

文字欄位
HTML語法 <input type="text" name="textName">
document.formName.textName.value
值的長度document.formName.textName.value.length
是否唯讀document.formName.textName.readOnly

文字區塊
HTML語法 <textarea name="textareaName" cols="32" rows="6"></textarea>
document.formName.textareaName.value
值的長度document.formName.textareaName.value.length
是否唯讀document.formName.textareaName.readOnly
寬度document.formName.textareaName.cols
高度document.formName.textareaName.rows

單選核取
HTML語法 <input type="radio" name="radioName" value="1">
共幾項document.formName.radioName.length
第N項的值document.formName.radioName[N].value
第N項核取?document.formName.radioName[N].checked
只有一項的值document.formName.radioName.text
只有一項核取?document.formName.radioName.checked

多選核取 (N由0開始計算)
HTML語法 <input type="checkbox" name="boxName" value="1">
共幾項document.formName.boxName.length
第N項的值document.formName.boxName[N].value
第N項核取?document.formName.boxme[N].checked
只有一項的值document.formName.boxName.text
只有一項核取?document.formName.boxe.checked

下拉清單 (N由0開始計算)
HTML語法 <select name="selectName">
<option value="value">text
</select>
共幾項document.formName.selectName.length
選到第N項document.formName.selectName.selectedIndex
第N項的值document.formName.selectName.options[N].value
第N項的文字document.formName.selectName.options[N].text

按鈕
HTML語法 <input type="submit" name="btnName">
<input type="button="btnName">
<input type="reset" name="btnName">
按鈕上的字document.formName.btnName.value
按鈕可不可按document.formName.btnName.disabled

沒有留言:

張貼留言