取得字串變數內的子字串
格式 %var:~m,n%
var 表字串變數名稱
m 為正值表前端要位移的字元數, 若 m 為負值則表由後面開始要選取的字元數(m=0表全部選取)
n 為經過前段處理(m 值的選取)後, 再選取的字元數, 若 n 為正值表選取字元的長度, 若 n 為負值則表由後面要刪除的字元數(n=0 表選取長度為0, 即不選取之意)
範例
設定參數
set line=1234567890ABCDEF
1. 位移10字元(~10)後取的 5 個字元
D:\>echo %line:~10,5%
ABCDE
2. 取字串最後10個字元
D:\>echo %line:~-10%
7890ABCDEF
3. 前端位移3個字元, 後端刪除4個字元
D:\>echo %line:~3,-4%
4567890AB
4. 先取後10個字元後, 再取4個長度的字元
D:\>echo %line:~-10,4%
7890
5. 先取後10個字元後, 刪除最後4個字元
D:\>echo %line:~-10,-4%
7890AB
2018年4月18日 星期三
2017年2月6日 星期一
VBA: 常用功能
## Application 級的功能
' 執行時停止螢幕異動
Application.ScreenUpdating = False
## 目前工作資料取得
' 取得目前活頁簿的工作目錄及名稱
ActiveWorkbook.Path
ActiveWorkbook.Name
' 取得目前執行VBS的活頁簿其工作目錄及名稱
ThisWorkbook.Path
ThisWorkbook.Name
' 取得活頁簿的工作目錄(檔案路徑)
strNowPath = Excel.ActiveWorkbook.Path
' 取得目前工作頁的名稱
szName = ThisWorkbook.ActiveSheet.Name
' 取得目前資料格的 row 值
endRow = ActiveCell.Row
' 取得 VBA 程式所在工作表內特定工作表的資料格內容
A = ThisWorkbook.Worksheets(strNameSheet).Cells(1, 1)
## 指定目前的有效視窗
Windows("D:\test\myfile.xls").Activate
## 新增指定名稱的工作表
Sheets.Add().Name = "TestOK"
Worksheets.Add(After:=Worksheets("ReadMe")).Name = "MySheet"
## 取得目前所有工作表的名稱
方式一
For Each thisSheet In Sheets
thisSheet.Name 的內容即為工作表名稱
Next thisSheet
方式二
For i = 1 To Worksheets.Count
Worksheets(i).Name 的內容即為工作表名稱
Next
## 刪除作用中的工作表
'關閉警告視窗
Application.DisplayAlerts = False
'刪除作用中的工作表
ActiveSheet.Delete
'恢復警告視窗
Application.DisplayAlerts = True
## 與工作表相關的指令
' 設定工作表是否可見
Worksheets(toSheet).Visible = True
' 設定為作用中的工作表
Worksheets(toSheet).Activate
' 選取工作表
Worksheets(toSheet).Select
' 刪除工作表資料內容
Worksheets(toSheet).Cells.Delete
' 選取目前活動工作表所有資料格並複製到剪貼簿
ActiveSheet.Cells.Select
Selection.Copy
' 將前項剪貼簿內容貼到目前活動工作表資料格內
' 請注意複製與貼上所選取的範圍宜相同
ActiveSheet.Cells.Select
ActiveSheet.Paste
' 設定欄寬
Columns("A:A").Select
Selection.ColumnWidth = 20
或
Columns("A:A").ColumnWidth = 20
' 隱藏工作表
Worksheets("mySheet").Visible = False
## 讀取或設定資料格的方式
m_year1 = Worksheets("Setting").Cells(5, 2)
Worksheets("Setting").Cells(5, 2) = 1234
' 設定資料格內容為公式
Sheets("總表_每日").Cells(myRow, 2).Formula = "=DSUM(DB_Open!$A:$E,5,$" & col(ii - 1) & "$2:$" & col(ii - 1) & "$3)"
## 找到有資料列的最後一列
Range("A65530").Select
'跳到該欄最後一列有資料的地方
Selection.End(xlUp).Select
'endRow 為最後一列有資料的列數
endRow = ActiveCell.Row
' 執行時停止螢幕異動
Application.ScreenUpdating = False
## 目前工作資料取得
' 取得目前活頁簿的工作目錄及名稱
ActiveWorkbook.Path
ActiveWorkbook.Name
' 取得目前執行VBS的活頁簿其工作目錄及名稱
ThisWorkbook.Path
ThisWorkbook.Name
' 取得活頁簿的工作目錄(檔案路徑)
strNowPath = Excel.ActiveWorkbook.Path
' 取得目前工作頁的名稱
szName = ThisWorkbook.ActiveSheet.Name
' 取得目前資料格的 row 值
endRow = ActiveCell.Row
' 取得 VBA 程式所在工作表內特定工作表的資料格內容
A = ThisWorkbook.Worksheets(strNameSheet).Cells(1, 1)
## 指定目前的有效視窗
Windows("D:\test\myfile.xls").Activate
## 新增指定名稱的工作表
Sheets.Add().Name = "TestOK"
Worksheets.Add(After:=Worksheets("ReadMe")).Name = "MySheet"
## 取得目前所有工作表的名稱
方式一
For Each thisSheet In Sheets
thisSheet.Name 的內容即為工作表名稱
Next thisSheet
方式二
For i = 1 To Worksheets.Count
Worksheets(i).Name 的內容即為工作表名稱
Next
## 刪除作用中的工作表
'關閉警告視窗
Application.DisplayAlerts = False
'刪除作用中的工作表
ActiveSheet.Delete
'恢復警告視窗
Application.DisplayAlerts = True
## 與工作表相關的指令
' 設定工作表是否可見
Worksheets(toSheet).Visible = True
' 設定為作用中的工作表
Worksheets(toSheet).Activate
' 選取工作表
Worksheets(toSheet).Select
' 刪除工作表資料內容
Worksheets(toSheet).Cells.Delete
' 選取目前活動工作表所有資料格並複製到剪貼簿
ActiveSheet.Cells.Select
Selection.Copy
' 將前項剪貼簿內容貼到目前活動工作表資料格內
' 請注意複製與貼上所選取的範圍宜相同
ActiveSheet.Cells.Select
ActiveSheet.Paste
' 設定欄寬
Columns("A:A").Select
Selection.ColumnWidth = 20
或
Columns("A:A").ColumnWidth = 20
' 隱藏工作表
Worksheets("mySheet").Visible = False
## 讀取或設定資料格的方式
m_year1 = Worksheets("Setting").Cells(5, 2)
Worksheets("Setting").Cells(5, 2) = 1234
' 設定資料格內容為公式
Sheets("總表_每日").Cells(myRow, 2).Formula = "=DSUM(DB_Open!$A:$E,5,$" & col(ii - 1) & "$2:$" & col(ii - 1) & "$3)"
## 找到有資料列的最後一列
Range("A65530").Select
'跳到該欄最後一列有資料的地方
Selection.End(xlUp).Select
'endRow 為最後一列有資料的列數
endRow = ActiveCell.Row
VBA: 檔案處理
## 檔案處理
## 開啟檔案
Workbooks.Open Filename:= _
"D:\20140325\test.xls"
## 關閉檔案
Windows(szOriginalFileName).Activate
ActiveWindow.Close False
## 使用循序模式寫入檔案()
Open "MyFile.txt" For Output As #1
For inputData = 1 To 4
Print #1, inputData
Next inputData
Close #1
## 使用循序模式讀取檔案
EX1.
Open "MyFile.txt" For Input As #1
For retu = 1 To 4
Input #1, x
Cells(1, retu) = x
Next retu
Close #1
EX2.
' Open the file for Input.
Open "TEXTFILE.TXT" For Input As #1
' Read each line of the text file into a single string
' variable.
Do While Not EOF(1)
Line Input #1, LineofText
MsgBox LineofText
Loop
' Close the file.
Close #1
## 讀取特定目錄內的檔名
Private Sub readFileName()
Dim szWorkPath As String ' 目前工作檔案目錄
Dim szFileName As String ' 讀到的檔案名稱
Dim szDirString As String ' DIR 搜尋檔案的字串
szWorkPath = ThisWorkbook.Path
szDirString = szWorkPath & "\data\*.pdf"
n = 0
'Sheet3.Cells.Delete '將之前的結果清除
szFileName = Dir(szDirString, vbDirectory)
szData = ""
While szFileName <> ""
n = n + 1
szData = szData + "##" + szFileName
szFileName = Dir() '讀取下一個檔案
Wend
MsgBox (n & szData)
End Sub
## 執行功能前的確認視窗
Dim szYesNo
szYesNo = MsgBox("確定執行發送功能 ?", vbYesNo, "發送視窗")
If szYesNo = vbYes Then
MsgBox "Hello"
Else
MsgBox "Naff Off"
End If
## 開啟檔案
Workbooks.Open Filename:= _
"D:\20140325\test.xls"
## 關閉檔案
Windows(szOriginalFileName).Activate
ActiveWindow.Close False
## 使用循序模式寫入檔案()
Open "MyFile.txt" For Output As #1
For inputData = 1 To 4
Print #1, inputData
Next inputData
Close #1
## 使用循序模式讀取檔案
EX1.
Open "MyFile.txt" For Input As #1
For retu = 1 To 4
Input #1, x
Cells(1, retu) = x
Next retu
Close #1
EX2.
' Open the file for Input.
Open "TEXTFILE.TXT" For Input As #1
' Read each line of the text file into a single string
' variable.
Do While Not EOF(1)
Line Input #1, LineofText
MsgBox LineofText
Loop
' Close the file.
Close #1
## 讀取特定目錄內的檔名
Private Sub readFileName()
Dim szWorkPath As String ' 目前工作檔案目錄
Dim szFileName As String ' 讀到的檔案名稱
Dim szDirString As String ' DIR 搜尋檔案的字串
szWorkPath = ThisWorkbook.Path
szDirString = szWorkPath & "\data\*.pdf"
n = 0
'Sheet3.Cells.Delete '將之前的結果清除
szFileName = Dir(szDirString, vbDirectory)
szData = ""
While szFileName <> ""
n = n + 1
szData = szData + "##" + szFileName
szFileName = Dir() '讀取下一個檔案
Wend
MsgBox (n & szData)
End Sub
## 執行功能前的確認視窗
Dim szYesNo
szYesNo = MsgBox("確定執行發送功能 ?", vbYesNo, "發送視窗")
If szYesNo = vbYes Then
MsgBox "Hello"
Else
MsgBox "Naff Off"
End If
VBA: 程式(控制流程與回圈)
VBA 控制流程與回圈
分享: 7Headlines facebook PLURK twitter
1. If-Then
If 條件 Then
敘述
End If
2. If-Then-Else
If 條件 Then
敘述
Else
敘述
End If
3. If-Then-ElseIf-Else
If 條件 Then
敘述
ElseIf 條件
敘述
ElseIf 條件
敘述
Else
敘述
End If
4. Select Case
Select Case 變數
Case 變數值 [to 變數值]
程式敘述一
…….
Case 變數值 [to 變數值]
程式敘述二
…….
[Case 變數值 [to 變數值]
程式敘述n-1
…….]
[Case Else
程式敘述n
…….]
End Select
5. Do While ...Loop
Do While 條件
敘述
Loop
6. Do Until… Loop
Do Until 條件
敘述
Loop
7. Do … Loop While
Do
敘述
Loop While 條件
8. Do … Loop Until
Do
敘述
Loop Until 條件
9. Do … Loop
Do
敘述
Loop
10. For … Next
For 數值變數 = 初始值 To 終止值 [Step 增量]
敘述
Next [數值變數]
11. For Each … Next
For Each 元素 In 群組
敘述
Next [元素]
12. While … Wend
While 條件
敘述
Wend
13. 跳離指令
*Exit Do:強制離開Do Loop迴圈。
*Exit For:強制離開For Next迴圈。
*Exit Sub:強制跳離Sub程序。
*Exit Function:強制跳離Function程序。
*Goto:需在程式碼前加上行號。
*End:無條件結束應用程式。
分享: 7Headlines facebook PLURK twitter
1. If-Then
If 條件 Then
敘述
End If
2. If-Then-Else
If 條件 Then
敘述
Else
敘述
End If
3. If-Then-ElseIf-Else
If 條件 Then
敘述
ElseIf 條件
敘述
ElseIf 條件
敘述
Else
敘述
End If
4. Select Case
Select Case 變數
Case 變數值 [to 變數值]
程式敘述一
…….
Case 變數值 [to 變數值]
程式敘述二
…….
[Case 變數值 [to 變數值]
程式敘述n-1
…….]
[Case Else
程式敘述n
…….]
End Select
5. Do While ...Loop
Do While 條件
敘述
Loop
6. Do Until… Loop
Do Until 條件
敘述
Loop
7. Do … Loop While
Do
敘述
Loop While 條件
8. Do … Loop Until
Do
敘述
Loop Until 條件
9. Do … Loop
Do
敘述
Loop
10. For … Next
For 數值變數 = 初始值 To 終止值 [Step 增量]
敘述
Next [數值變數]
11. For Each … Next
For Each 元素 In 群組
敘述
Next [元素]
12. While … Wend
While 條件
敘述
Wend
13. 跳離指令
*Exit Do:強制離開Do Loop迴圈。
*Exit For:強制離開For Next迴圈。
*Exit Sub:強制跳離Sub程序。
*Exit Function:強制跳離Function程序。
*Goto:需在程式碼前加上行號。
*End:無條件結束應用程式。
VBA: function
VBA Function What It Does
Abs Returns the absolute value of a number
Array Returns a variant that contains an array
Asc Converts the first character of string to its ASCII value
Atn Returns the arctangent of a number
CBool Converts an expression to Boolean data type
CByte Converts an expression to byte data type
CCur Converts an expression to currency data type
CDate Converts an expression to date data type
CDbl Converts an expression to double data type
CDec Converts an expression to decimal data type
Choose Selects and returns a value from a list of arguments
Chr Converts an ANSI value to a character
CInt Converts an expression to integer data type
CLng Converts an expression to long data type
Cos Returns the cosine of a number
CreateObject Creates an OLE Automation object
CSng Converts an expression to single data type
CStr Converts an expression to string data type
CurDir Returns the current path
CVar Converts an expression to variant data type
CVDate Converts an expression to date data type
CVErr Returns a user-defined error type
Date Returns the current system date
DateAdd Returns a date with a specific date interval added to it
DateDiff Returns the difference between two dates as a time interval
DatePart Returns an integer containing a specific part of a date
DateSerial Returns a date for a specified year, month, and day
DateValue Converts a string to date
Day Returns the day of the month of a date
Dir Returns the name of a file or directory that matches a pattern
DoEvents Yields execution so the operating system can process other events
EOF Returns True if the end of a text file has been reached
Error Returns the error message that corresponds to an error number
Exp Returns the base of the natural logarithms (e) raised to a power
FileAttr Returns the file mode for a text file
FileDateTime Returns the date and time when a file was last modified
FileLen Returns the number of bytes in a file
Fix Returns the integer portion of a number
Format Returns an expression in a particular format
FormatCurrency Returns a number as a string, formatted as currency
FormatDateTime Returns a number as a string, formatted as a date and/or time
FormatNumber Returns a number as a formatted string
FormatPercent Returns a number as a string, formatted as a percentage
FreeFile Returns the next file number available for use by the Open statement
GetAll Returns a list of key settings and their values (originally created with SaveSetting) from an application’s entry in the Windows registry
GetAttr Returns a code representing a file attribute
GetObject Retrieves an OLE Automation object from a file
GetSetting Returns a key setting value from an application’s entry in the Windows registry
Hex Converts from decimal to hexadecimal
Hour Returns the hour of a time
IIf Returns one of two parts, depending on the evaluation of an expression
Input Returns a specific number of characters from an open text file
InputBox Displays a box to prompt a user for input, and returns the value entered
InStr Returns the position of a string within another string
InStrRev Returns the position of a string within another string, beginning at the back end of the string
Int Returns the integer portion of a number
IsArray Returns True if a variable is an array
IsDate Returns True if a variable is a date
IsEmpty Returns True if a variable has not been initialized
IsError Returns True if an expression is an error value
IsMissing Returns True if an optional argument was not passed to a Procedure
IsNull Returns True if an expression contains no valid data
IsNumeric Returns True if an expression can be evaluated as a number
IsObject Returns True if an expression references an OLE Automation object
Join Returns a string created by joining a number of substrings contained in an array
LBound Returns the lower bound of an array
LCase Returns a string converted to lowercase
Left Returns a specified number of characters from the left of a string
Len Returns the length of a string, in characters
Loc Returns the current read or write position of a text file
LOF Returns the number of bytes in an open text file
Log Returns the natural logarithm of a number
LTrim Returns a copy of a string with no leading spaces
Mid Returns a specified number of characters from a string
MidB Returns a specified number of bytes from a specified position in a string string
Minute Returns the minute of a time
Month Returns the month of a date
MonthName Returns a string indicating the specified month
MsgBox Displays a modal message box and returns the ID of the button clicked
Now Returns the current system date and time
Oct Converts from decimal to octal
Replace Returns a string in which one substring is replaced with another
RGB Returns a number representing an RGB color value
Right Returns a specified number of characters from the right of a string
Rnd Returns a random number between 0 and 1
Round Rounds a number to a specific number of decimal places
RTrim Returns a copy of a string with no trailing spaces
Second Returns the second of a time
Seek Returns the current position in a text file
Sgn Returns an integer that indicates the sign of a number
Shell Runs an executable program
Sin Returns the sine of a number
Space Returns a string with a specified number of spaces
Split Returns an array consisting of a number of substrings
Sqr Returns the square root of a number
Str Returns a string representation of a number
StrComp Returns a value indicating the result of a string comparison
StrConv Returns a string variant converted as specified
String Returns a repeating character or string
StrReverse Returns the characters of a string in reverse order
Switch Evaluates a list of expressions and returns a value associated with the first expression in the list that is True
Tab Positions output in an output stream
Tan Returns the tangent of a number
Time Returns the current system time
Timer Returns the number of seconds since midnight
TimeSerial Returns the time for a specified hour, minute, and second
TimeValue Converts a string to a time serial number
Trim Returns a string without leading and spaces and replaces multiple spaces with a single space
TypeName Returns a string that describes the data type of a variable
UBound Returns the upper bound of an array
UCase Converts a string to uppercase
Val Returns the numbers contained in a string
VarType Returns a value indicating the subtype of a variable
Weekday Returns a number representing a day of the week
Weekday Name Returns a string indicating the specified weekday
Year Returns the year of a date
Abs Returns the absolute value of a number
Array Returns a variant that contains an array
Asc Converts the first character of string to its ASCII value
Atn Returns the arctangent of a number
CBool Converts an expression to Boolean data type
CByte Converts an expression to byte data type
CCur Converts an expression to currency data type
CDate Converts an expression to date data type
CDbl Converts an expression to double data type
CDec Converts an expression to decimal data type
Choose Selects and returns a value from a list of arguments
Chr Converts an ANSI value to a character
CInt Converts an expression to integer data type
CLng Converts an expression to long data type
Cos Returns the cosine of a number
CreateObject Creates an OLE Automation object
CSng Converts an expression to single data type
CStr Converts an expression to string data type
CurDir Returns the current path
CVar Converts an expression to variant data type
CVDate Converts an expression to date data type
CVErr Returns a user-defined error type
Date Returns the current system date
DateAdd Returns a date with a specific date interval added to it
DateDiff Returns the difference between two dates as a time interval
DatePart Returns an integer containing a specific part of a date
DateSerial Returns a date for a specified year, month, and day
DateValue Converts a string to date
Day Returns the day of the month of a date
Dir Returns the name of a file or directory that matches a pattern
DoEvents Yields execution so the operating system can process other events
EOF Returns True if the end of a text file has been reached
Error Returns the error message that corresponds to an error number
Exp Returns the base of the natural logarithms (e) raised to a power
FileAttr Returns the file mode for a text file
FileDateTime Returns the date and time when a file was last modified
FileLen Returns the number of bytes in a file
Fix Returns the integer portion of a number
Format Returns an expression in a particular format
FormatCurrency Returns a number as a string, formatted as currency
FormatDateTime Returns a number as a string, formatted as a date and/or time
FormatNumber Returns a number as a formatted string
FormatPercent Returns a number as a string, formatted as a percentage
FreeFile Returns the next file number available for use by the Open statement
GetAll Returns a list of key settings and their values (originally created with SaveSetting) from an application’s entry in the Windows registry
GetAttr Returns a code representing a file attribute
GetObject Retrieves an OLE Automation object from a file
GetSetting Returns a key setting value from an application’s entry in the Windows registry
Hex Converts from decimal to hexadecimal
Hour Returns the hour of a time
IIf Returns one of two parts, depending on the evaluation of an expression
Input Returns a specific number of characters from an open text file
InputBox Displays a box to prompt a user for input, and returns the value entered
InStr Returns the position of a string within another string
InStrRev Returns the position of a string within another string, beginning at the back end of the string
Int Returns the integer portion of a number
IsArray Returns True if a variable is an array
IsDate Returns True if a variable is a date
IsEmpty Returns True if a variable has not been initialized
IsError Returns True if an expression is an error value
IsMissing Returns True if an optional argument was not passed to a Procedure
IsNull Returns True if an expression contains no valid data
IsNumeric Returns True if an expression can be evaluated as a number
IsObject Returns True if an expression references an OLE Automation object
Join Returns a string created by joining a number of substrings contained in an array
LBound Returns the lower bound of an array
LCase Returns a string converted to lowercase
Left Returns a specified number of characters from the left of a string
Len Returns the length of a string, in characters
Loc Returns the current read or write position of a text file
LOF Returns the number of bytes in an open text file
Log Returns the natural logarithm of a number
LTrim Returns a copy of a string with no leading spaces
Mid Returns a specified number of characters from a string
MidB Returns a specified number of bytes from a specified position in a string string
Minute Returns the minute of a time
Month Returns the month of a date
MonthName Returns a string indicating the specified month
MsgBox Displays a modal message box and returns the ID of the button clicked
Now Returns the current system date and time
Oct Converts from decimal to octal
Replace Returns a string in which one substring is replaced with another
RGB Returns a number representing an RGB color value
Right Returns a specified number of characters from the right of a string
Rnd Returns a random number between 0 and 1
Round Rounds a number to a specific number of decimal places
RTrim Returns a copy of a string with no trailing spaces
Second Returns the second of a time
Seek Returns the current position in a text file
Sgn Returns an integer that indicates the sign of a number
Shell Runs an executable program
Sin Returns the sine of a number
Space Returns a string with a specified number of spaces
Split Returns an array consisting of a number of substrings
Sqr Returns the square root of a number
Str Returns a string representation of a number
StrComp Returns a value indicating the result of a string comparison
StrConv Returns a string variant converted as specified
String Returns a repeating character or string
StrReverse Returns the characters of a string in reverse order
Switch Evaluates a list of expressions and returns a value associated with the first expression in the list that is True
Tab Positions output in an output stream
Tan Returns the tangent of a number
Time Returns the current system time
Timer Returns the number of seconds since midnight
TimeSerial Returns the time for a specified hour, minute, and second
TimeValue Converts a string to a time serial number
Trim Returns a string without leading and spaces and replaces multiple spaces with a single space
TypeName Returns a string that describes the data type of a variable
UBound Returns the upper bound of an array
UCase Converts a string to uppercase
Val Returns the numbers contained in a string
VarType Returns a value indicating the subtype of a variable
Weekday Returns a number representing a day of the week
Weekday Name Returns a string indicating the specified weekday
Year Returns the year of a date
2016年6月15日 星期三
CSS: 選擇器
CSS3 選擇器
在 CSS 中,選擇器是一種模式,用於選擇需要添加樣式的元素。"CSS" 列指示該屬性是在哪個 CSS 版本中定義的。(CSS1、CSS2 還是 CSS3。)
| 選擇器 | 例子 | 例子描述 | CSS |
|---|---|---|---|
| .class | .intro | 選擇 class="intro" 的所有元素。 | 1 |
| #id | #firstname | 選擇 id="firstname" 的所有元素。 | 1 |
| * | * | 選擇所有元素。 | 2 |
| element | p | 選擇所有 <p> 元素。 | 1 |
| element,element | div,p | 選擇所有 <div> 元素和所有 <p> 元素。 | 1 |
| element element | div p | 選擇 <div> 元素內部的所有 <p> 元素。 | 1 |
| element>element | div>p | 選擇父元素為 <div> 元素的所有 <p> 元素。 | 2 |
| element+element | div+p | 選擇緊接在 <div> 元素之後的所有 <p> 元素。 | 2 |
| [attribute] | [target] | 選擇帶有 target 屬性所有元素。 | 2 |
| [attribute=value] | [target=_blank] | 選擇 target="_blank" 的所有元素。 | 2 |
| [attribute~=value] | [title~=flower] | 選擇 title 屬性包含單詞 "flower" 的所有元素。 | 2 |
| [attribute|=value] | [lang|=en] | 選擇 lang 屬性值以 "en" 開頭的所有元素。 | 2 |
| :link | a:link | 選擇所有未被訪問的鏈接。 | 1 |
| :visited | a:visited | 選擇所有已被訪問的鏈接。 | 1 |
| :active | a:active | 選擇活動鏈接。 | 1 |
| :hover | a:hover | 選擇鼠標指針位於其上的鏈接。 | 1 |
| :focus | input:focus | 選擇獲得焦點的 input 元素。 | 2 |
| :first-letter | p:first-letter | 選擇每個 <p> 元素的首字母。 | 1 |
| :first-line | p:first-line | 選擇每個 <p> 元素的首行。 | 1 |
| :first-child | p:first-child | 選擇屬於父元素的第一個子元素的每個 <p> 元素。 | 2 |
| :before | p:before | 在每個 <p> 元素的內容之前插入內容。 | 2 |
| :after | p:after | 在每個 <p> 元素的內容之後插入內容。 | 2 |
| :lang(language) | p:lang(it) | 選擇帶有以 "it" 開頭的 lang 屬性值的每個 <p> 元素。 | 2 |
| element1~element2 | p~ul | 選擇前面有 <p> 元素的每個 <ul> 元素。 | 3 |
| [attribute^=value] | a[src^="https"] | 選擇其 src 屬性值以 "https" 開頭的每個 <a> 元素。 | 3 |
| [attribute$=value] | a[src$=".pdf"] | 選擇其 src 屬性以 ".pdf" 結尾的所有 <a> 元素。 | 3 |
| [attribute*=value] | a[src*="abc"] | 選擇其 src 屬性中包含 "abc" 子串的每個 <a> 元素。 | 3 |
| :first-of-type | p:first-of-type | 選擇屬於其父元素的首個 <p> 元素的每個 <p> 元素。 | 3 |
| :last-of-type | p:last-of-type | 選擇屬於其父元素的最後 <p> 元素的每個 <p> 元素。 | 3 |
| :only-of-type | p:only-of-type | 選擇屬於其父元素唯一的 <p> 元素的每個 <p> 元素。 | 3 |
| :only-child | p:only-child | 選擇屬於其父元素的唯一子元素的每個 <p> 元素。 | 3 |
| :nth-child(n) | p:nth-child(2) | 選擇屬於其父元素的第二個子元素的每個 <p> 元素。 | 3 |
| :nth-last-child(n) | p:nth-last-child(2) | 同上,從最後一個子元素開始計數。 | 3 |
| :nth-of-type(n) | p:nth-of-type(2) | 選擇屬於其父元素第二個 <p> 元素的每個 <p> 元素。 | 3 |
| :nth-last-of-type(n) | p:nth-last-of-type(2) | 同上,但是從最後一個子元素開始計數。 | 3 |
| :last-child | p:last-child | 選擇屬於其父元素最後一個子元素每個 <p> 元素。 | 3 |
| :root | :root | 選擇文檔的根元素。 | 3 |
| :empty | p:empty | 選擇沒有子元素的每個 <p> 元素(包括文本節點)。 | 3 |
| :target | #news:target | 選擇當前活動的 #news 元素。 | 3 |
| :enabled | input:enabled | 選擇每個啟用的 <input> 元素。 | 3 |
| :disabled | input:disabled | 選擇每個禁用的 <input> 元素 | 3 |
| :checked | input:checked | 選擇每個被選中的 <input> 元素。 | 3 |
| :not(selector) | :not(p) | 選擇非 <p> 元素的每個元素。 | 3 |
| ::selection | ::selection | 選擇被用戶選取的元素部分。 | 3 |
2016年4月28日 星期四
Android: 繪圖
## 簡單圖形繪製
protected void onDraw(Canvas canvas) {//重寫的繪製方法
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);//繪製黑色背景
Paint paint = new Paint();//建立畫筆
paint.setColor(Color.WHITE);//設置畫筆顏色為紅色
canvas.drawRect(10, 10, 110, 110, paint);//繪製矩形
canvas.drawText("這是字符串", 10, 130, paint);//字符串,以字符串下面為基準
RectF rf1 = new RectF(10, 130, 110, 230);//定義一個矩形
canvas.drawArc(rf1, 0, 45, true, paint);//畫弧,順時針
canvas.drawLine(150, 10, 250, 110, paint);//畫線
RectF rf2 = new RectF(150, 130, 250, 230);//定義一個矩形
canvas.drawOval(rf2, paint);//畫圓
}
## 貼圖功能
public void initBitmap(){
paint = new Paint();//建立一個畫筆
myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img);//獲得圖片資源
}
@Override
protected void onDraw(Canvas canvas) {//重寫的繪製方法
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setAntiAlias(true);//打開抗鋸齒
paint.setColor(Color.WHITE);//設置畫筆的顏色
paint.setTextSize(15);
canvas.drawBitmap(myBitmap, 10, 10, paint);//繪製圖片
canvas.save(); //儲存畫布
Matrix m1=new Matrix();
m1.setTranslate(500, 10); // 平移
Matrix m2=new Matrix();
m2.setRotate(15); // 旋轉
Matrix m3=new Matrix();
m3.setConcat(m1, m2);
m1.setScale(0.8f, 0.8f);
m2.setConcat(m3, m1);
canvas.drawBitmap(myBitmap, m2, paint);
canvas.restore();
canvas.save();
paint.setAlpha(180);
m1.setTranslate(200,100);
m2.setScale(1.3f, 1.3f);
m3.setConcat(m1, m2);
canvas.drawBitmap(myBitmap, m3, paint);
paint.reset();
canvas.restore();
paint.setTextSize(40);
paint.setColor(0xffFFFFFF);
canvas.drawText("圖片的寬度: "+myBitmap.getWidth(), 20, 380, paint);//繪製字符串,圖片的寬度
canvas.drawText("圖片的高度: "+myBitmap.getHeight(), 20, 430, paint);//繪製字符串,圖片的高度
paint.reset();
}
2016年4月20日 星期三
JS: JavaScript 陣列也可這樣用
今天看到一段程式碼, 覺得很有趣於是吧它記錄下來, 如下
var p=[];
p.push('r='+Math.random().toString(36).substring(7));
p.push('w='+screen.width);
p.push('h='+screen.height);
var a=p.join('&');
alert("a = " + a);
p=[]; 是宣告一個空陣列
p.push 是於陣列內放入一個值
p.join('&') 是得到一個將陣列內容以 & 號串聯後的新字串
Math.random().toString(36).substring(7) 是什麼啊
我們知到 Math.random() 是得到一個0~1之間的數值, 加上.toString(36)後是將該數值轉換為36進位的字串, 36進位每個數值是0~9及a~z的字符, 所以由數值亂數轉換成字串亂數, 厲害吧! 因為0.xx的數值轉換為字串後還是以 0.xx 開頭所以再用.substring(7) 將前面幾個數字去掉, 成為名符其實的0~9及a~z所組合的亂字串
另外透過瀏覽器的 screen 物件可直接取得裝置螢幕的寛高資訊
var p=[];
p.push('r='+Math.random().toString(36).substring(7));
p.push('w='+screen.width);
p.push('h='+screen.height);
var a=p.join('&');
alert("a = " + a);
p=[]; 是宣告一個空陣列
p.push 是於陣列內放入一個值
p.join('&') 是得到一個將陣列內容以 & 號串聯後的新字串
Math.random().toString(36).substring(7) 是什麼啊
我們知到 Math.random() 是得到一個0~1之間的數值, 加上.toString(36)後是將該數值轉換為36進位的字串, 36進位每個數值是0~9及a~z的字符, 所以由數值亂數轉換成字串亂數, 厲害吧! 因為0.xx的數值轉換為字串後還是以 0.xx 開頭所以再用.substring(7) 將前面幾個數字去掉, 成為名符其實的0~9及a~z所組合的亂字串
另外透過瀏覽器的 screen 物件可直接取得裝置螢幕的寛高資訊
2016年4月15日 星期五
CSS: CSS3 新增的屬性
由於目前部份屬性在各瀏覽器內尚未統一, 故該可能會有以該瀏覽器為字首的屬性名稱, 以代表該瀏覽器的支援程度
其常用瀏覽器字首如下
-webkit- : 指 Webkit 系列的瀏覽器, 包括 Safari 或 Chrome 等
-moz- : 指 Gecko 系列的瀏覽器, 包括 Mozilla 或 Firefox 等
-o- : 指 Opera 瀏覽器
-ms- : 指 Microsoft IE 瀏覽器
== 圓角(border-radius)的屬性
屬性名稱 : -webkit-border-radius 及 -moz-border-radius
設定值為圓角的半徑值(可分別設定四個圓角的半徑, 其順序為左上, 右上, 右下, 左下)
利用區塊長, 寬及圓角等屬性的變化, 可設定出圓, 半圓, 1/4 圓等區塊
EX:
#circle_100 {
position:absolute;
left:50px;
top:50px;
background-color: #888;
width:200px;
height:200px;
-webkit-border-radius:100px;
-moz-border-radius:100px;
}
== 加陰影的屬性
區塊陰影
屬性名稱 : box-shadow, -webkit-box-shadow 及 -moz-box-shadow
設定值有四個參數, 分別為X偏量, Y偏量, 模糊半徑, 陰影顏色
文字陰影
屬性名稱 : text-shadow
設定值有四個參數, 分別為X偏量, Y偏量, 模糊半徑, 陰影顏色
以上陰影的參數值以四個參數為一組, 可同時設定多組(各組參數以,號分隔), 以達陰影層次上的效果
EX:
text-shadow:0 0 4px #ccc, 0 -5px 4px #ff3, 2px -10px 6px #fd3, -2px -15px 11px #f80, 2px -19px 18px #f20;
== 變形(transform)的屬性
變形的屬性為 transform, -webkit-transform(chrome 瀏覽器適用) 或 -moz-transform(firefox 瀏覽器適用)
== CSS3 新增顏色的函數
1. rgba(red, green, blue, alpha) 函數於顏色中增加了透明度的設定
alpha 為透明度參數, 其值介於 0.0(完全透明) 至 1.0(完全不透明)間
2. 漸層的顏色
漸層顏色主要分為線性漸層及放射漸層等兩類, 但由於各瀏覽器對漸層顏色函數格式尚未統一, 故不同瀏覽器可能會使用不同格式的函數, 其顯示效果也有所不同
目前主要兩大瀏覽器陣營定義漸層顏色函數名稱及參數格式如下
WebKit系列瀏覽器(如 Safari 或 Chrome)
只有一個函數, 以參數來區分漸層類型
第一個參數為 linear 時表 [線性漸層] 參數如下
-webkit-gradient(linear, 開始點位置, 結束點位置, 顏色順序參數...);
EX: -webkit-gradient(linear, left top, right bottom, from(#06f), to(#fff));
第一個參數為 radial 時表 [放射漸層] 參數如下(圓形的放射)
-webkit-gradient(linear, 開始圓心點位置, 開始半徑, 結束圓心點位置, 結束半徑, 顏色順序參數...);
EX: -webkit-gradient(radial, center center, 10, center center, 90, from(red), to(#1E90FF), color-stop(70%, yellow));
點位置或圓心點位置的參數是由兩個參數值所組合而成, 第一個值代表 X-軸位置, 第二個值代表 Y-軸位置, 其值可以為數值, 百分號 或 left top center 等文字標示
顏色順序至少要包含[開始顏色]及[結束顏色]兩個參數, 也可在中間增加其顏色參數值, 其顏色參數值需使用from(), to() 或 color-stop()等函數設定
設定顏色的函數說明如下
color-stop(百分比, 顏色) : 可設定不同百分比位置的漸層顏色值, 百分比為0表開始位置的顏色, 百分比為1表結束位置的顏色
from(顏色) 設定開始顏色 = color-stop(0, 顏色)
to(顏色) 設定結束顏色 = color-stop(1, 顏色)
Gecko 系列瀏覽器(如Mozilla 或 Firefox)
以不同的函數定義不同的漸層類型
-moz-linear-gradient 函式為 [線性漸層], 格式為 -moz-linear-gradient(起點及方向, 各顏色變化值);
-moz-radial-gradient 函式為 [放射漸層], 格式為 -moz-radial-gradient(放射型狀, 各顏色變化值);
[起點及方向]是指起始位置和方向, 其值可為 left(表由左向右漸層), top(表由上向下漸層), left top(表由左上向右下漸層), bottom 45deg(表由下方向45度角方向漸層)
[各顏色變化值] 是顏色變化的順序值如 red, yellow 30%, blue 90%, white 一組顏色的變化, 若有加上 % 者表到達該百分比位置時的顏色
[放射型狀]是指放射漸層的型狀, 可為圓形放射(circle)或橢圓放射(ellipse)
其常用瀏覽器字首如下
-webkit- : 指 Webkit 系列的瀏覽器, 包括 Safari 或 Chrome 等
-moz- : 指 Gecko 系列的瀏覽器, 包括 Mozilla 或 Firefox 等
-o- : 指 Opera 瀏覽器
-ms- : 指 Microsoft IE 瀏覽器
== 圓角(border-radius)的屬性
屬性名稱 : -webkit-border-radius 及 -moz-border-radius
設定值為圓角的半徑值(可分別設定四個圓角的半徑, 其順序為左上, 右上, 右下, 左下)
利用區塊長, 寬及圓角等屬性的變化, 可設定出圓, 半圓, 1/4 圓等區塊
EX:
#circle_100 {
position:absolute;
left:50px;
top:50px;
background-color: #888;
width:200px;
height:200px;
-webkit-border-radius:100px;
-moz-border-radius:100px;
}
== 加陰影的屬性
區塊陰影
屬性名稱 : box-shadow, -webkit-box-shadow 及 -moz-box-shadow
設定值有四個參數, 分別為X偏量, Y偏量, 模糊半徑, 陰影顏色
文字陰影
屬性名稱 : text-shadow
設定值有四個參數, 分別為X偏量, Y偏量, 模糊半徑, 陰影顏色
以上陰影的參數值以四個參數為一組, 可同時設定多組(各組參數以,號分隔), 以達陰影層次上的效果
EX:
text-shadow:0 0 4px #ccc, 0 -5px 4px #ff3, 2px -10px 6px #fd3, -2px -15px 11px #f80, 2px -19px 18px #f20;
== 變形(transform)的屬性
變形的屬性為 transform, -webkit-transform(chrome 瀏覽器適用) 或 -moz-transform(firefox 瀏覽器適用)
其屬性值有下
旋轉 : rotate(15deg) 順時針旋轉15度
縮放 : scaleX(1.5)x方向放大到1.5倍, scaleY(0.5) y方向縮小0.5倍,scale(1.5, 0.5) 同時設定 x方向放大到1.5倍 y方向縮小0.5倍
傾斜 : skewX(10deg)x方向傾斜10度, skewY(10deg)y方向傾斜10度, skew(10deg, 10deg) 同時設定 x方向及y方向各傾斜10度;
== CSS3 新增顏色的函數
1. rgba(red, green, blue, alpha) 函數於顏色中增加了透明度的設定
alpha 為透明度參數, 其值介於 0.0(完全透明) 至 1.0(完全不透明)間
2. 漸層的顏色
漸層顏色主要分為線性漸層及放射漸層等兩類, 但由於各瀏覽器對漸層顏色函數格式尚未統一, 故不同瀏覽器可能會使用不同格式的函數, 其顯示效果也有所不同
目前主要兩大瀏覽器陣營定義漸層顏色函數名稱及參數格式如下
WebKit系列瀏覽器(如 Safari 或 Chrome)
只有一個函數, 以參數來區分漸層類型
第一個參數為 linear 時表 [線性漸層] 參數如下
-webkit-gradient(linear, 開始點位置, 結束點位置, 顏色順序參數...);
EX: -webkit-gradient(linear, left top, right bottom, from(#06f), to(#fff));
第一個參數為 radial 時表 [放射漸層] 參數如下(圓形的放射)
-webkit-gradient(linear, 開始圓心點位置, 開始半徑, 結束圓心點位置, 結束半徑, 顏色順序參數...);
EX: -webkit-gradient(radial, center center, 10, center center, 90, from(red), to(#1E90FF), color-stop(70%, yellow));
點位置或圓心點位置的參數是由兩個參數值所組合而成, 第一個值代表 X-軸位置, 第二個值代表 Y-軸位置, 其值可以為數值, 百分號 或 left top center 等文字標示
顏色順序至少要包含[開始顏色]及[結束顏色]兩個參數, 也可在中間增加其顏色參數值, 其顏色參數值需使用from(), to() 或 color-stop()等函數設定
設定顏色的函數說明如下
color-stop(百分比, 顏色) : 可設定不同百分比位置的漸層顏色值, 百分比為0表開始位置的顏色, 百分比為1表結束位置的顏色
from(顏色) 設定開始顏色 = color-stop(0, 顏色)
to(顏色) 設定結束顏色 = color-stop(1, 顏色)
Gecko 系列瀏覽器(如Mozilla 或 Firefox)
以不同的函數定義不同的漸層類型
-moz-linear-gradient 函式為 [線性漸層], 格式為 -moz-linear-gradient(起點及方向, 各顏色變化值);
-moz-radial-gradient 函式為 [放射漸層], 格式為 -moz-radial-gradient(放射型狀, 各顏色變化值);
[起點及方向]是指起始位置和方向, 其值可為 left(表由左向右漸層), top(表由上向下漸層), left top(表由左上向右下漸層), bottom 45deg(表由下方向45度角方向漸層)
[各顏色變化值] 是顏色變化的順序值如 red, yellow 30%, blue 90%, white 一組顏色的變化, 若有加上 % 者表到達該百分比位置時的顏色
[放射型狀]是指放射漸層的型狀, 可為圓形放射(circle)或橢圓放射(ellipse)
CSS: CSS 樣式的屬性
== 設定邊界(margin)的屬性
margin-top 設定上邊界
margin-right 設定右邊界
margin-bottom 設定下邊界
margin-left 設定左邊界
margin 可同時設定四個邊界(margin:[上邊界] [右邊界] [下邊界] [左邊界])
== 設定邊框(border)的屬性
border-style(屬性值有solid-實線;dashed-虛線;double-雙線;dotted-點線;groove-凹線;ridge-凸線;inset-嵌入線;outset-浮出線)
border-width
border-color
border-top-, border-left-, border-bottom-, border-right- : -後可接 style, width 或 color 以設定單邊的屬性
border 同時設定四個邊框的屬性
== 設定留白(padding)的屬性
padding-top (上)
padding-right (右)
padding-bottom (下)
padding-left (左)
padding 同時設定四邊留白的屬性(padding:[上留白] [右留白] [下留白] [左留白])
== 設定背景(background)的屬性
background-color : 背景顏色
background-image ; 背景圖片
background-repeat : 設定背景圖片是否重覆, 預設 repeat(x 及 y方向均重覆), 其他值有 x-repeat(x-方向重複)、y-repeat(y-方向重複)及 no-repeat(不重複)
background-attachment : 指定背景圖案是否固定在螢幕上, 其值有 fixed(固定不動) 及 scroll (跟著移動)
background-position : 用來指定背景圖案的位置, 有兩個參數
第一個參數設定水平位置(x方向), 可為 left,center,right,百分比或px數值
第二個參數設定垂直位置(y方向), 可為 top,center,bottom,百分比或px數值
註:background-repeat值預設是重覆, 也就是以圖片填滿視窗, 此時 background-attachment 及 background-position 的屬性均無效
CSS3 以後還增加了調整圖片尺寸的屬性
background-size 或 -webkit-background-size(chrome 瀏覽器使用) 或 -moz-background-size(firefox 瀏覽器使用), 其參數是以寛和高兩個數值(100px 80px)或百分比(80% 40%)
EX:
background-size:50% 50%;
-moz-background-size:50% 50%;
-webkit-background-size:50% 50%;
== 設定顏色(color)的屬性
color : 顏色屬性值的設定有三種方式
十六進位值, 如 {color:#XXXXXX;}
RGB 碼, 如 {color:rgb(X,Y,Z);}
顏色名稱, 如 {color:red;}
CSS3 以後顏色可用 rgba() 函數設定(增加透明度的值)
== 設定字體(font)的屬性
font-family
font-size
font-weight : 字的粗細度(可為100~900的數值或 bold(厚)、bolder(更厚)、及 normal(正常))
font-style
font-variant
== 設定 <A> 標籤 連接狀態的屬性
a:link : 設定當連接過去的網頁尚未被看過時,該連接的樣式。
a:visited : 設定當連接過去的網頁已經看過時,該連接的樣式。
a:hover : 設定當滑鼠蓋在連接上面時,該連接的樣式。
a:active : 設定當連接被點擊時,該連接的樣式。
== 設定清單(list)的屬性( <ul><li> 標籤的屬性)
list-style-type : 設定清單中每個項目前的標記符號, 其值有
none(沒有);disc(全黑圓圈);circle(空心圓圈);square(正方形)
或以upper-latin(大寫拉丁文);lower-latin(小寫拉丁文);upper-roman(大寫羅馬文);lower-roman(小寫羅馬文);upper-alpha(大寫希臘文);lower-alpha(小寫希臘文)等文字的數值編號
list-style-position : 設定標記符號是否為是文字的一部份(決定同一清單項目換行時是否有內縮的情形)
其屬性值有inside(是文字的一部份) 及 outside(否是文字的一部份), 預定值為 outside
list-style-image : 設定以特定圖案做為標記符號, 設定方式為 list-style-image:url([image_url]);
list-style : 直接以本項屬性設定前三項屬性, 如 list-style: url("circle.gif") none inside; 其三個參數沒有順序性
== 設定表格(table)的屬性
在 HTML 中 <table> 標籤內常有 border、cellpadding 及 cellspacing 等屬性, 這些屬性均可透過CSS方式直接設定
另一般常用的文字、字體、邊框、顏色 及 背景等屬也可在 table、th、tr 及 td 等選擇器內直接引用
== 與位置(position)相關的屬性
position : 用於設定元件放置位置的類型, 其值有
static(預設值): 表元素依預設位置擺放(設為 statics 時其 top、bottom、left 和 right 等屬性值均無效)
absolute: 表元素依 top、bottom、left 和 right 等設定值與區塊內的絕對位置擺放
relative: 表元素依 top、bottom、left 和 right 等設定值與區塊內預設位置的相對位置擺放
fixed:表元素依 top、bottom、left 和 right 等設定值在瀏覽器的絕對位置擺放
top, right, left, bottom : 設定四邊位置的擺放值, 其值可以是長度、百分比 或是 auto
overflow : 用於設定當內容放不下時的處理方式, 其值包含
visible : 內容完全呈現,不管放得下放不下。
hidden : 放不下的內容就不顯示出來。
scroll : 無論內容放得下放不下,都加入上下捲軸及左右捲軸的功能。
auto : 當內容放不下時,加上捲軸的功能。
z-index : 用於設定元素重疊時的順序(Z軸), z-index 值比較大的元素會被放在上面
== 與文字樣式相關的屬性
direction : 設定文字的方向, 其值包括 ltr(靠左向右排列) 及 rtl(靠右向左排列)
letter-spacing : 設定每個字與字的間隙
line-height : 設定兩行起點之間的距離(行高)
text-align : 設定文字對齊方式, 其值包括 left(靠左對齊), right(靠右對齊), center(置中), justify(左右對齊)
text-decoration : 設定文字的裝飾樣式, 其值包括 underline(下邊線), overline(上邊線), line-through(中間線), blink(閃爍)
text-indent : 設定每段第一行前段間所要保留的空間, 其設定值可為百分比或長度
text-transform : 設定文字大小寫的展現方式, 其值包括 capitalize(每個字的第一個字母都大寫, 其他字母不變), uppercase(所有字母都大寫), lowercase(所有字母都小寫), none(不改變)
word-spacing : 設定每個字與每個字之間的距離。
== 浮動(float)與清除(clear)的屬性
float : 用於設定文字在圖的左邊或右邊浮動的狀態, 其值包含left、right 和 none
clear : 當設定 float 屬性時可用 clear 來清除原 float 所設定的狀態
== 滑鼠游標圖案的屬性
cursor 屬性可用於設定滑鼠游標的圖案, 其設定值有 crosshair, pointer, text, move, wait, help, progress, not-allowed, no-drop, no-vertical-text, all-scroll, col-resize, row-resize, e-resize, ne-resize, n-resize, nw-resize, w-resize, sw-resize, s-resize, se-resize
另外也可以使用{cursor:url(圖案網址);語法自定游標圖案
margin-top 設定上邊界
margin-right 設定右邊界
margin-bottom 設定下邊界
margin-left 設定左邊界
margin 可同時設定四個邊界(margin:[上邊界] [右邊界] [下邊界] [左邊界])
== 設定邊框(border)的屬性
border-style(屬性值有solid-實線;dashed-虛線;double-雙線;dotted-點線;groove-凹線;ridge-凸線;inset-嵌入線;outset-浮出線)
border-width
border-color
border-top-, border-left-, border-bottom-, border-right- : -後可接 style, width 或 color 以設定單邊的屬性
border 同時設定四個邊框的屬性
== 設定留白(padding)的屬性
padding-top (上)
padding-right (右)
padding-bottom (下)
padding-left (左)
padding 同時設定四邊留白的屬性(padding:[上留白] [右留白] [下留白] [左留白])
== 設定背景(background)的屬性
background-color : 背景顏色
background-image ; 背景圖片
background-repeat : 設定背景圖片是否重覆, 預設 repeat(x 及 y方向均重覆), 其他值有 x-repeat(x-方向重複)、y-repeat(y-方向重複)及 no-repeat(不重複)
background-attachment : 指定背景圖案是否固定在螢幕上, 其值有 fixed(固定不動) 及 scroll (跟著移動)
background-position : 用來指定背景圖案的位置, 有兩個參數
第一個參數設定水平位置(x方向), 可為 left,center,right,百分比或px數值
第二個參數設定垂直位置(y方向), 可為 top,center,bottom,百分比或px數值
註:background-repeat值預設是重覆, 也就是以圖片填滿視窗, 此時 background-attachment 及 background-position 的屬性均無效
CSS3 以後還增加了調整圖片尺寸的屬性
background-size 或 -webkit-background-size(chrome 瀏覽器使用) 或 -moz-background-size(firefox 瀏覽器使用), 其參數是以寛和高兩個數值(100px 80px)或百分比(80% 40%)
EX:
background-size:50% 50%;
-moz-background-size:50% 50%;
-webkit-background-size:50% 50%;
color : 顏色屬性值的設定有三種方式
十六進位值, 如 {color:#XXXXXX;}
RGB 碼, 如 {color:rgb(X,Y,Z);}
顏色名稱, 如 {color:red;}
CSS3 以後顏色可用 rgba() 函數設定(增加透明度的值)
== 設定字體(font)的屬性
font-family
font-size
font-weight : 字的粗細度(可為100~900的數值或 bold(厚)、bolder(更厚)、及 normal(正常))
font-style
font-variant
== 設定 <A> 標籤 連接狀態的屬性
a:link : 設定當連接過去的網頁尚未被看過時,該連接的樣式。
a:visited : 設定當連接過去的網頁已經看過時,該連接的樣式。
a:hover : 設定當滑鼠蓋在連接上面時,該連接的樣式。
a:active : 設定當連接被點擊時,該連接的樣式。
== 設定清單(list)的屬性( <ul><li> 標籤的屬性)
list-style-type : 設定清單中每個項目前的標記符號, 其值有
none(沒有);disc(全黑圓圈);circle(空心圓圈);square(正方形)
或以upper-latin(大寫拉丁文);lower-latin(小寫拉丁文);upper-roman(大寫羅馬文);lower-roman(小寫羅馬文);upper-alpha(大寫希臘文);lower-alpha(小寫希臘文)等文字的數值編號
list-style-position : 設定標記符號是否為是文字的一部份(決定同一清單項目換行時是否有內縮的情形)
其屬性值有inside(是文字的一部份) 及 outside(否是文字的一部份), 預定值為 outside
list-style-image : 設定以特定圖案做為標記符號, 設定方式為 list-style-image:url([image_url]);
list-style : 直接以本項屬性設定前三項屬性, 如 list-style: url("circle.gif") none inside; 其三個參數沒有順序性
== 設定表格(table)的屬性
在 HTML 中 <table> 標籤內常有 border、cellpadding 及 cellspacing 等屬性, 這些屬性均可透過CSS方式直接設定
另一般常用的文字、字體、邊框、顏色 及 背景等屬也可在 table、th、tr 及 td 等選擇器內直接引用
== 與位置(position)相關的屬性
position : 用於設定元件放置位置的類型, 其值有
static(預設值): 表元素依預設位置擺放(設為 statics 時其 top、bottom、left 和 right 等屬性值均無效)
absolute: 表元素依 top、bottom、left 和 right 等設定值與區塊內的絕對位置擺放
relative: 表元素依 top、bottom、left 和 right 等設定值與區塊內預設位置的相對位置擺放
fixed:表元素依 top、bottom、left 和 right 等設定值在瀏覽器的絕對位置擺放
top, right, left, bottom : 設定四邊位置的擺放值, 其值可以是長度、百分比 或是 auto
overflow : 用於設定當內容放不下時的處理方式, 其值包含
visible : 內容完全呈現,不管放得下放不下。
hidden : 放不下的內容就不顯示出來。
scroll : 無論內容放得下放不下,都加入上下捲軸及左右捲軸的功能。
auto : 當內容放不下時,加上捲軸的功能。
z-index : 用於設定元素重疊時的順序(Z軸), z-index 值比較大的元素會被放在上面
== 與文字樣式相關的屬性
direction : 設定文字的方向, 其值包括 ltr(靠左向右排列) 及 rtl(靠右向左排列)
letter-spacing : 設定每個字與字的間隙
line-height : 設定兩行起點之間的距離(行高)
text-align : 設定文字對齊方式, 其值包括 left(靠左對齊), right(靠右對齊), center(置中), justify(左右對齊)
text-decoration : 設定文字的裝飾樣式, 其值包括 underline(下邊線), overline(上邊線), line-through(中間線), blink(閃爍)
text-indent : 設定每段第一行前段間所要保留的空間, 其設定值可為百分比或長度
text-transform : 設定文字大小寫的展現方式, 其值包括 capitalize(每個字的第一個字母都大寫, 其他字母不變), uppercase(所有字母都大寫), lowercase(所有字母都小寫), none(不改變)
word-spacing : 設定每個字與每個字之間的距離。
== 浮動(float)與清除(clear)的屬性
float : 用於設定文字在圖的左邊或右邊浮動的狀態, 其值包含left、right 和 none
clear : 當設定 float 屬性時可用 clear 來清除原 float 所設定的狀態
== 滑鼠游標圖案的屬性
cursor 屬性可用於設定滑鼠游標的圖案, 其設定值有 crosshair, pointer, text, move, wait, help, progress, not-allowed, no-drop, no-vertical-text, all-scroll, col-resize, row-resize, e-resize, ne-resize, n-resize, nw-resize, w-resize, sw-resize, s-resize, se-resize
另外也可以使用{cursor:url(圖案網址);語法自定游標圖案
2016年4月14日 星期四
CSS: 認識 CSS
CSS(Cascading Style Sheet)是一種串接樣式表(Stylesheet), 用來描述 HTML 資料要如何顯現的語言
== 樣式使用分類:
1. 行內式 : 直接定義在原有 HTML 的標籤內
2. 內崁式 : 定義在 HEAD 標籤內, 並設定於 <style type="text/css">..</style> 宣告內
3. 連結式 : 定義在外部檔案內, 使用 <link> 標籤連結外部檔案
== 宣告 CSS 樣式的語法
選擇器 {
屬性:設定值;
...
}
其中選擇器主要有三種:
1. 型別(Type)選擇器 : 型別選擇器是直接針對(X)HTML標籤設定樣式的選擇器
2. Class 選擇器 : 以.號帶領選擇器名稱者為 Class 選擇器, 可 HTML 標籤內使用 class= 的方式引用
另可針對特定標籤設定 Class 選擇器, 如 b.special = {..}, 只針對<b class='special'> 才有效
若未指定標籤(直接以.號開頭), 則提供所有標籤引用
同一個標籤也可套用多個不同的 Class 選擇器, 如 <b class='style_1 style_2'> 表於 <b> 標籤內套用 style_1 及 style_2 兩種樣式(以空白區分)
3. ID 選擇器 : 以#號帶領選擇器名稱者為 ID 選擇器, 是提供 HTML 內特定 ID 的物件所引用之樣式
註:Class 名稱及 ID 名稱都是有大小寫區分的。所以 .classone 及 .ClassOne 是代表兩個不同的 Class 選擇器。
== 樣式的繼承性:
在 HTML 文件中, 當標籤間有親子關係時(在外的為父標籤, 在內的為子標籤), 其子標籤未明確定義的樣式會直接繼承父標籤所設定的樣式
EX:
body { color: #FF0000; font-size:20px;}
p { color: #00FF00; }
strong { font-size:50px; }
<body>
<p> P標籤為父 <strong>strong標籤為子時</strong> 的樣式</p>
<strong> strong標籤為父<p> P標籤為子時</p> 的樣式</strong>
</body>
其中 [P標籤為父] 時定義了顏色為綠色未定義字體大小, 所以是以<body>所定義的字體(20px)及綠色來顯示
而 [strong標籤為子時] 只修改了字體大小, 未修改顏色, 所以是以繼承父標籤的綠色來顯示
相反的 [strong標籤為父] 時只定義字體大小, 未定義顏色, 所以是以<body>所定義的紅色及自定義的字體(50px)來顯示
而 [P標籤為子時] 只修改顏色未修改字體大小, 所以是以繼承父標籤的字體大小(50px)來顯示
== CSS 樣式模型
盒子模式(box model)是 CSS 描述元素組成的重要觀念。
其盒子模式的式樣有四層:
1. 邊界(margin) : 最外層的界限, 可用來設定各個元素間之距離。
2. 邊框(border) : 設定邊框樣式
3. 留白(padding) : 內容到邊框的距離
4. 內容(content) : 是最內層的物件項目
== HTML 中 <Div> 與 <Span> 之差異
<Div> 與 <Span> 標籤均可於文件內標示出獨立的區域, 以利針對特定區域引用不同的 CSS 樣式
唯兩者所定義出的獨立的區域略有差別
<Div> 所定義的獨立區域區為塊級的範圍, 而 <Span> 所定義的區域則為元素級(如一行文字中的部份字串)
== 樣式使用分類:
1. 行內式 : 直接定義在原有 HTML 的標籤內
2. 內崁式 : 定義在 HEAD 標籤內, 並設定於 <style type="text/css">..</style> 宣告內
3. 連結式 : 定義在外部檔案內, 使用 <link> 標籤連結外部檔案
== 宣告 CSS 樣式的語法
選擇器 {
屬性:設定值;
...
}
其中選擇器主要有三種:
1. 型別(Type)選擇器 : 型別選擇器是直接針對(X)HTML標籤設定樣式的選擇器
2. Class 選擇器 : 以.號帶領選擇器名稱者為 Class 選擇器, 可 HTML 標籤內使用 class= 的方式引用
另可針對特定標籤設定 Class 選擇器, 如 b.special = {..}, 只針對<b class='special'> 才有效
若未指定標籤(直接以.號開頭), 則提供所有標籤引用
同一個標籤也可套用多個不同的 Class 選擇器, 如 <b class='style_1 style_2'> 表於 <b> 標籤內套用 style_1 及 style_2 兩種樣式(以空白區分)
3. ID 選擇器 : 以#號帶領選擇器名稱者為 ID 選擇器, 是提供 HTML 內特定 ID 的物件所引用之樣式
註:Class 名稱及 ID 名稱都是有大小寫區分的。所以 .classone 及 .ClassOne 是代表兩個不同的 Class 選擇器。
== 樣式的繼承性:
在 HTML 文件中, 當標籤間有親子關係時(在外的為父標籤, 在內的為子標籤), 其子標籤未明確定義的樣式會直接繼承父標籤所設定的樣式
EX:
body { color: #FF0000; font-size:20px;}
p { color: #00FF00; }
strong { font-size:50px; }
<body>
<p> P標籤為父 <strong>strong標籤為子時</strong> 的樣式</p>
<strong> strong標籤為父<p> P標籤為子時</p> 的樣式</strong>
</body>
其中 [P標籤為父] 時定義了顏色為綠色未定義字體大小, 所以是以<body>所定義的字體(20px)及綠色來顯示
而 [strong標籤為子時] 只修改了字體大小, 未修改顏色, 所以是以繼承父標籤的綠色來顯示
相反的 [strong標籤為父] 時只定義字體大小, 未定義顏色, 所以是以<body>所定義的紅色及自定義的字體(50px)來顯示
而 [P標籤為子時] 只修改顏色未修改字體大小, 所以是以繼承父標籤的字體大小(50px)來顯示
== CSS 樣式模型
盒子模式(box model)是 CSS 描述元素組成的重要觀念。
其盒子模式的式樣有四層:
1. 邊界(margin) : 最外層的界限, 可用來設定各個元素間之距離。
2. 邊框(border) : 設定邊框樣式
3. 留白(padding) : 內容到邊框的距離
4. 內容(content) : 是最內層的物件項目
== HTML 中 <Div> 與 <Span> 之差異
<Div> 與 <Span> 標籤均可於文件內標示出獨立的區域, 以利針對特定區域引用不同的 CSS 樣式
唯兩者所定義出的獨立的區域略有差別
<Div> 所定義的獨立區域區為塊級的範圍, 而 <Span> 所定義的區域則為元素級(如一行文字中的部份字串)
PHP: PHP 簡介
PHP 腳本在服務器上執行,然後向瀏覽器發送回純 HTML 結果。
== 基礎語法
PHP 腳本可放置於文檔中的任何位置。
PHP 腳本以 <?php 開頭,以 ?> 結尾:
PHP 的變數有大小寫之分, 但對所有自定函數、類和關鍵字(例如 if、else、echo 等等), 則不分大小寫
PHP 語句以分號結尾(;)
PHP 有三種注釋方式:
// 這是單行注釋
# 這也是單行注釋
/* 這是多行注釋塊 */
== PHP 變數(變數規則)
以 $ 符號開頭,其後是變數的名稱
名稱必須以字母或下劃線開頭(不能以數字開頭)
名稱只能包含字母數字字符和下劃線(A-z、0-9 以及 _)
變數名稱有大小寫區別($x 與 $X 是兩個不同的變數)
== PHP 數據類型
字串 : 字串是指引號內的任何文字。您可以使用單引號或雙引號來設定字串
註:用單引號是將引號內的文字直接設定為字串, 若用雙引號設定則會先將引號內的變數轉換為變數內容後的值為字串
整數 : 可以用三種格式規定整數:十進制、十六進制(前綴是 0x)或八進制(前綴是 0)
浮點數 : 有小數點或指數形式的數字。
邏輯 : 只有 true 及 false 兩個值
陣列 : 以 array 定義陣列, 其陣列中各元素可存放不同數據類型的值
類別(Class) : 由屬性及方法的組合
NULL : 代表變量無值, NULL 是數據類型 NULL 的唯一值。
== PHP 常量
常量是單一固定值的標識符號, 在腳本中無法改變該值。
有效的常量名以字符或下劃線開頭, 且貫穿整個腳本, 屬全域的標識符號。
常量使用 define() 函數設定, 其包含三個參數:
第 1 個參數定義常量的名稱
第 2 個參數定義常量的值
第 3 個參數是屬選用的參數, 代表該常量名是否區分大小寫。預設是 false。
== 基礎語法
PHP 腳本可放置於文檔中的任何位置。
PHP 腳本以 <?php 開頭,以 ?> 結尾:
PHP 的變數有大小寫之分, 但對所有自定函數、類和關鍵字(例如 if、else、echo 等等), 則不分大小寫
PHP 語句以分號結尾(;)
PHP 有三種注釋方式:
// 這是單行注釋
# 這也是單行注釋
/* 這是多行注釋塊 */
== PHP 變數(變數規則)
以 $ 符號開頭,其後是變數的名稱
名稱必須以字母或下劃線開頭(不能以數字開頭)
名稱只能包含字母數字字符和下劃線(A-z、0-9 以及 _)
變數名稱有大小寫區別($x 與 $X 是兩個不同的變數)
== PHP 數據類型
字串 : 字串是指引號內的任何文字。您可以使用單引號或雙引號來設定字串
註:用單引號是將引號內的文字直接設定為字串, 若用雙引號設定則會先將引號內的變數轉換為變數內容後的值為字串
整數 : 可以用三種格式規定整數:十進制、十六進制(前綴是 0x)或八進制(前綴是 0)
浮點數 : 有小數點或指數形式的數字。
邏輯 : 只有 true 及 false 兩個值
陣列 : 以 array 定義陣列, 其陣列中各元素可存放不同數據類型的值
類別(Class) : 由屬性及方法的組合
NULL : 代表變量無值, NULL 是數據類型 NULL 的唯一值。
== PHP 常量
常量是單一固定值的標識符號, 在腳本中無法改變該值。
有效的常量名以字符或下劃線開頭, 且貫穿整個腳本, 屬全域的標識符號。
常量使用 define() 函數設定, 其包含三個參數:
第 1 個參數定義常量的名稱
第 2 個參數定義常量的值
第 3 個參數是屬選用的參數, 代表該常量名是否區分大小寫。預設是 false。
PHP: PHP 變數
== 變數
變數是由錢號($)開始,後面接著變數名稱,變數名稱有大小寫之分。
變數名稱可以是英文字母、數字、底線等字元所組成,但不能是數字開頭。
PHP中的變數並不需要特別宣告,所有未宣告的變數都視為NULL,變數的型別是直接依指派內容來決定。
PHP提供傳址的功能, 使用&號代表該變數的位址
EX: $aa = &$bb; 其中變數 $aa 是存著變數 $bb 的位址(兩個變數指向同一個位址)
== 變數範圍
程式中我們可能會在不同的位置,用到相同的變數名稱,表示不同的東西,如果變數沒有範圍就可能會互相干擾。所以在程式中,會有不同的變數範圍,當程式離開這個範圍之後,屬於此範圍的變數就會清除。
PHP的變數分為全域變數和區域變數,不同使用(宣告)的位置來決定有效範圍, 其原則如下:
1. 在 function 中的變數為區域變數。
2. 在 include 中的變數範圍與呼叫 include 的所在範圍相同。
3. 其他為全域變數。
== 全域變數
PHP 的全域變數會儲存在 $GLOBALS 的陣列變數之中
若設定 $aa='Hello!'; 則會在 $GLOBALS 陣列中增加一個 { 'aa' => 'Hello' }的元素
所以全域變數可使用 $GLOBALS['aa'] 的名稱來代替 $aa
註:在 function 中出現的變數均為區域變數, 若要使用全域變數則需先使用 global 的關鍵字宣告, 或直接使用 $GLOBALS 來存取全域變數的內容
其實在函數中使用 global $aa; 宣告與直接使用 $GLOBALS['aa'] 還是有點差異
因為 global $aa; 的宣告相當於是 $aa = &$GLOBALS['aa']; 即宣告一個aa的區域變數, 並指向全域變數aa的位址, 而使 $GLOBALS['aa'] 則是直接使用全域變數aa
== 靜態變數
PHP 使用 static 關鍵字來宣告靜態變數
靜態變數會一直存在,直到程式結束
靜態變數特性如下:
1. 靜態變數的指派必須要直接給一個定值(不可為運算式、變數、函式結果或物件)
2. 重複的宣告靜態變數時,以最後一次宣告的值為初始值
== 可變變數(變數的變數)
PHP提供可變變數的功能, 就是在一個字串變數名稱前加上$號, 則會以原變數內容做為新變數的名稱
其使用規則如下:
1. 使用錢號($)加上變數、常數或函式回傳值等來轉換,並可以多重使用。
2. 與字串中的變數類似,可使用大括號來明確標示出來源。
3. 不能用在類別和函式中的超級全域變數和$this關鍵字。
EX1:
$RED='紅';
$BLUE='籃';
$GREEN='綠';
$COLORS=array('RED', 'BLUE', 'GREEN');
for ($x=0; $x<3; $x++) {
//$var=$COLORS[$x];
echo $COLORS[$x] . '字是:' . $$COLORS[$x] . '<br>';
}
EX2:
$number_1='One'; $number_2='Two'; $number_3='Three';
for ($x=1; $x<=3; $x++) {
$var='number_' . $x;
echo $var . ' 的數值是:' . $$var . '<br>';
}
== 外部來源變數
Server 端接收 Client 端透過表單、網址列傳值或Cookie等方式傳送的資料。會依據來源而儲存在各別的預定義變數內
其變數包括 $_POST、$_GET、$_COOKIE、$_REQUEST和$_FILES(儲存上傳的檔案)。
註1:檔案上傳必須使用POST方法其 POST 內容與上傳檔案大小限制是在 php.ini 設定,其設定參數分別為 post_max_size 和 upload_max_filesize。
註2:Cookie是存放在使用者的瀏覽器,除了JavaScript外,也可以由PHP的setcookie()函式去設定,然後使用$_COOKIE去取得。
註2:$_GET、$_POST和$_COOKIE等使用者送出的請求(Request),所以會將其合併到$_REQUEST陣列內,其要合併的資料項目,定義在php.ini中的request_order或variables_order參數內, 其參數內各字元代表值如下
E: $_ENV
G: $_GET
P: $_POST
C: $_COOKIE
S: $_SERVER
變數是由錢號($)開始,後面接著變數名稱,變數名稱有大小寫之分。
變數名稱可以是英文字母、數字、底線等字元所組成,但不能是數字開頭。
PHP中的變數並不需要特別宣告,所有未宣告的變數都視為NULL,變數的型別是直接依指派內容來決定。
PHP提供傳址的功能, 使用&號代表該變數的位址
EX: $aa = &$bb; 其中變數 $aa 是存著變數 $bb 的位址(兩個變數指向同一個位址)
== 變數範圍
程式中我們可能會在不同的位置,用到相同的變數名稱,表示不同的東西,如果變數沒有範圍就可能會互相干擾。所以在程式中,會有不同的變數範圍,當程式離開這個範圍之後,屬於此範圍的變數就會清除。
PHP的變數分為全域變數和區域變數,不同使用(宣告)的位置來決定有效範圍, 其原則如下:
1. 在 function 中的變數為區域變數。
2. 在 include 中的變數範圍與呼叫 include 的所在範圍相同。
3. 其他為全域變數。
== 全域變數
PHP 的全域變數會儲存在 $GLOBALS 的陣列變數之中
若設定 $aa='Hello!'; 則會在 $GLOBALS 陣列中增加一個 { 'aa' => 'Hello' }的元素
所以全域變數可使用 $GLOBALS['aa'] 的名稱來代替 $aa
註:在 function 中出現的變數均為區域變數, 若要使用全域變數則需先使用 global 的關鍵字宣告, 或直接使用 $GLOBALS 來存取全域變數的內容
其實在函數中使用 global $aa; 宣告與直接使用 $GLOBALS['aa'] 還是有點差異
因為 global $aa; 的宣告相當於是 $aa = &$GLOBALS['aa']; 即宣告一個aa的區域變數, 並指向全域變數aa的位址, 而使 $GLOBALS['aa'] 則是直接使用全域變數aa
== 靜態變數
PHP 使用 static 關鍵字來宣告靜態變數
靜態變數會一直存在,直到程式結束
靜態變數特性如下:
1. 靜態變數的指派必須要直接給一個定值(不可為運算式、變數、函式結果或物件)
2. 重複的宣告靜態變數時,以最後一次宣告的值為初始值
== 可變變數(變數的變數)
PHP提供可變變數的功能, 就是在一個字串變數名稱前加上$號, 則會以原變數內容做為新變數的名稱
其使用規則如下:
1. 使用錢號($)加上變數、常數或函式回傳值等來轉換,並可以多重使用。
2. 與字串中的變數類似,可使用大括號來明確標示出來源。
3. 不能用在類別和函式中的超級全域變數和$this關鍵字。
EX1:
$RED='紅';
$BLUE='籃';
$GREEN='綠';
$COLORS=array('RED', 'BLUE', 'GREEN');
for ($x=0; $x<3; $x++) {
//$var=$COLORS[$x];
echo $COLORS[$x] . '字是:' . $$COLORS[$x] . '<br>';
}
EX2:
$number_1='One'; $number_2='Two'; $number_3='Three';
for ($x=1; $x<=3; $x++) {
$var='number_' . $x;
echo $var . ' 的數值是:' . $$var . '<br>';
}
== 外部來源變數
Server 端接收 Client 端透過表單、網址列傳值或Cookie等方式傳送的資料。會依據來源而儲存在各別的預定義變數內
其變數包括 $_POST、$_GET、$_COOKIE、$_REQUEST和$_FILES(儲存上傳的檔案)。
註1:檔案上傳必須使用POST方法其 POST 內容與上傳檔案大小限制是在 php.ini 設定,其設定參數分別為 post_max_size 和 upload_max_filesize。
註2:Cookie是存放在使用者的瀏覽器,除了JavaScript外,也可以由PHP的setcookie()函式去設定,然後使用$_COOKIE去取得。
註2:$_GET、$_POST和$_COOKIE等使用者送出的請求(Request),所以會將其合併到$_REQUEST陣列內,其要合併的資料項目,定義在php.ini中的request_order或variables_order參數內, 其參數內各字元代表值如下
E: $_ENV
G: $_GET
P: $_POST
C: $_COOKIE
S: $_SERVER
2016年4月13日 星期三
PHP: PHP 特殊指令函數說明
== @ini_set()
ini_set是用來設置php.ini內參數的值,在函數執行的時候生效,腳本結束後,設置失效, 無需打開php.ini文件,就能修改配置。
函數格式:string ini_set(string $varname, string $newvalue)
EX:
@ ini_set('memory_limit', '64M');
== date_default_timezone_set() 函數
設置用在腳本中所有日期/時間函數的默認時區。
EX:
date_default_timezone_set( 'UTC' );
== var_dump()
用於顯示變數的類型及值, 如
$colors=array( 0 => 100, 'aa' => 'test', 1 => 'OK' );
var_dump($colors);
執行後顯示
array(3) { [0]=> int(100) ["aa"]=> string(4) "test" [1]=> string(2) "OK" }
ini_set是用來設置php.ini內參數的值,在函數執行的時候生效,腳本結束後,設置失效, 無需打開php.ini文件,就能修改配置。
函數格式:string ini_set(string $varname, string $newvalue)
EX:
@ ini_set('memory_limit', '64M');
== date_default_timezone_set() 函數
設置用在腳本中所有日期/時間函數的默認時區。
EX:
date_default_timezone_set( 'UTC' );
== var_dump()
用於顯示變數的類型及值, 如
$colors=array( 0 => 100, 'aa' => 'test', 1 => 'OK' );
var_dump($colors);
執行後顯示
array(3) { [0]=> int(100) ["aa"]=> string(4) "test" [1]=> string(2) "OK" }
HTML5: HTML5 的新標籤
HTML5 的新標籤
== 語意及結構的新標籤
HTML5 提供新的[標籤], 使文件在語意及結構上更加明確與完整
<header>
頭部,主要是用來放置網站的刊頭,固定會重覆出現的導覽、LOGO、站名等內容。
<footer>
尾部,包括頁尾,如版權聲明等
註:<header>及<footer>也可置於<section>區塊內作為[章節]的標頭與標尾
<hgroup>
用於將標題與副標題群組在一起(<h> Tag 的 Group)
<nav>
導覽列,像是選單之類用來導引使用到網站各個地方的,可以用<nav> 包起來,在裝置上的操作是可以直接跳過、跳是跳到這個段落然後跳選下一個連結。
<aside>
側欄,網頁中非主要區域的地方,常見放廣告及導覽、全站檢索的地方。
<article>
文章,意指一篇完整而可獨立抽出來閱讀的文章。
<section>
章節,表示相關但是不同的段落
註:章節tag內可以包含<article>(文章), 文章tag內也可以包含<section>(章節), 用於表達不同的語意。
== 其他新標籤
在圖片上加入說明文字的標籤
在<figure>內加入<figcaption>標籤, 可於<figure>所指定的圖片下方顯示說明文字
加強[文字]的標籤
<mark> 標籤可產生如螢光筆的效果, 以加強[文字]的顯示
影片與音樂播放的標籤
使用 <video> 標籤可插入 [影片] 檔案
使用 <audio> 標籤可插入 [音樂] 檔案
<video> 與 <audio> 標籤使用方式與屬性均相同, 範例如下
<video width="640" height="368" preload controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogv" type='video/ogg; codecs="theora, vorbis"'>
</video>
<video> 與 <audio> 標籤的屬性
width 設置視頻(音頻)播放器的寬度。
height 設置視頻(音頻)播放器高度。
autoplay 設定視頻(音頻)就緒後立即播放。
controls 設定顯示播放控制按鈕的控件。
loop 設定重複播放。
muted 設定音頻輸出先設定為靜音。
poster 設定視頻(音頻)下載或無法顯示時所顯示的圖像。
preload 設定視頻(音頻)在頁面下載時即開始預先於背景下載,並預備播放。
註:使用 "autoplay" 屬性時,則忽略 preload 屬性。
src 設定要播放視頻(音頻)的 URL。
繪圖用的標籤
使用 <canvas> 標籤可建立畫布提供繪製各種圖形
<canvas> 標籤主要是建立一塊畫布, 所以單獨使用 <canvas> 標籤是無法呈現圖形的, 還必須配合在 JavaScript 內運用繪圖函數於畫布上繪製, 才可顯現圖形
== 語意及結構的新標籤
HTML5 提供新的[標籤], 使文件在語意及結構上更加明確與完整
<header>
頭部,主要是用來放置網站的刊頭,固定會重覆出現的導覽、LOGO、站名等內容。
<footer>
尾部,包括頁尾,如版權聲明等
註:<header>及<footer>也可置於<section>區塊內作為[章節]的標頭與標尾
<hgroup>
用於將標題與副標題群組在一起(<h> Tag 的 Group)
<nav>
導覽列,像是選單之類用來導引使用到網站各個地方的,可以用<nav> 包起來,在裝置上的操作是可以直接跳過、跳是跳到這個段落然後跳選下一個連結。
<aside>
側欄,網頁中非主要區域的地方,常見放廣告及導覽、全站檢索的地方。
<article>
文章,意指一篇完整而可獨立抽出來閱讀的文章。
<section>
章節,表示相關但是不同的段落
註:章節tag內可以包含<article>(文章), 文章tag內也可以包含<section>(章節), 用於表達不同的語意。
== 其他新標籤
在圖片上加入說明文字的標籤
在<figure>內加入<figcaption>標籤, 可於<figure>所指定的圖片下方顯示說明文字
加強[文字]的標籤
<mark> 標籤可產生如螢光筆的效果, 以加強[文字]的顯示
影片與音樂播放的標籤
使用 <video> 標籤可插入 [影片] 檔案
使用 <audio> 標籤可插入 [音樂] 檔案
<video> 與 <audio> 標籤使用方式與屬性均相同, 範例如下
<video width="640" height="368" preload controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogv" type='video/ogg; codecs="theora, vorbis"'>
</video>
<video> 與 <audio> 標籤的屬性
width 設置視頻(音頻)播放器的寬度。
height 設置視頻(音頻)播放器高度。
autoplay 設定視頻(音頻)就緒後立即播放。
controls 設定顯示播放控制按鈕的控件。
loop 設定重複播放。
muted 設定音頻輸出先設定為靜音。
poster 設定視頻(音頻)下載或無法顯示時所顯示的圖像。
preload 設定視頻(音頻)在頁面下載時即開始預先於背景下載,並預備播放。
註:使用 "autoplay" 屬性時,則忽略 preload 屬性。
src 設定要播放視頻(音頻)的 URL。
繪圖用的標籤
使用 <canvas> 標籤可建立畫布提供繪製各種圖形
<canvas> 標籤主要是建立一塊畫布, 所以單獨使用 <canvas> 標籤是無法呈現圖形的, 還必須配合在 JavaScript 內運用繪圖函數於畫布上繪製, 才可顯現圖形
2016年4月12日 星期二
RegExp: 正則表達式
Regular Expression (正則表達式 or REGEXP) 在本質上是一種小型的特用語言。這種
語言主要是用來做字串的比對、刪除、替換或拷貝重製等工作。
== 字元分類
字元 – 指ASCII 0~255的所有字元和中文字元(也包中文括符號字元)
數字-- 指0-9的字元(數字是ASCII字元的一部份)
字母 --指英文字母,指 abc….z AB…Z(英文字母是ASCII字元的一部份)
字 -- 字是指被符號、空白隔開的文數字串,指 0-9 a-z A-Z 英文底線以及中文字元(不包括中文字符)
英文字 -- 特別指英文的word,
文數字 -- 指英文字母,中文字元和0-9的數字字元的總稱
白字 -- whitespace
隱藏在資料中空白、換行、跳格(tab)等控制字元, 以”\”加上一個字母來表示如
== 控制字元定義
\t Tab ascii 碼 09($09) , 水平跳格
\n new line feed ascii 碼 10 ($0A) , 跳下一行
\v Vertical tab 碼 11($0B) 垂直跳格
\f form feed ascii 碼 14 ($0C), 換到下一頁
\r carrige return ascii碼15 ($0D), 回車,回到一行的開始
\a Bell 碼 07($07) 鈴聲控制字元.
\e ASCII Escape character 碼 27 ($1B), ESC 鍵
\0 變數名稱, 代表先前的整個 REGEXP 子式的全部內容, 不必被( )所包住
\1 - \9 變數名稱, 代表先前 REGEXP 子式內(…) 所搜到的內容將依序被放入了 \1 \2 … \9
== 保留字元
. ^ $ * + ? { } [ ] \ | ( ) 等為正則表達式的保留字元, 若在搜尋樣版中是要用來表達本來的字元時, 則要在這個字元前
面加上"\"的跳脫符號, 各保留字的定義如下
. 指除了\n以外的所有字元
^ 位於[]內的第一個字元表"非"的意思, 如[^abc] 指除了a或b或c以外的字元
^ 在搜尋使用時表搜尋"開頭"相同的字串
$ 在搜尋使用時表搜尋"結尾"相同的字串
{} 用來定義重複的次數, 如 {n}表n次, {2,5}表2~5次, {,5}表0~5次, {5,}表5到無限次
* 指前一個字元出現 0 或 多次的重複, 即{0,}
+ 指前一個字元至少出現 1 次(1 或 多次的重複), 即{1,}
? 指前一個字元出現 0 或 1 次, 即{0,1}, 如搜尋 colou?r 則 color 及 colour 均會找到
[] 設定字元群組
\ - 為"跳脫字元", 用來指定控制字元時使用
| 或(OR)的意思, 也就是以 | 的兩邊的字串做樣版比對搜尋,只要有一個符合, 就是成功
() 設定"字串樣本"時使用, 其設定的樣本會存放在 \1 ~ \9 的變數內
(?...) 當()號內第一個字元為?時則不是"字串樣本"
== 表達式所使用的符號
[] - 指在[]號內的任一字元符合, 如表達式為 [abc]y 時則, ay, by, cy 等均符合
[^] - 指在[^]號內以外的任一字元符合, 如表達式為 [^ab]y 時則, 除了 ay, by 以外的 ?y 均符合
== 字族名稱
為了簡化表示, 因此定義部份字族名稱來表式某特定的字元群組
\d 或 [:digit:] 指0~9的數字, 就是[0-9]
\u 或 [:upper:] 指大寫英文字母, 就是[A-Z]
\l 或 [:lower:] 指小寫英文字母, 就是[a-z]
\w 或 [:word:] 指所有文數字加上”_”底線, 就是[_A-Za-z_0-9]
\s 或 [:space:] 指白字, 如 [\t\n\r\f] 等
\b 所有英文字的邊界字元, 如 空白字元等
\D 或 [^[:digit:]] 指除了0~9以外的字元, 就是[^0-9]
\U 或 [^[:upper:]] 指除了大寫英文字母以外的字元, 就是[^A-Z]
\L 或 [^[:lower:]] 指除了小寫英文字母以外的字元, 就是[^a-z]
\W 或 [^[:word:]] 指非文字,也就是\w字元族以外的所有字元
\S 或 [^[:space:]] 指非白字,也就是\s以外的字元
\B 所有非英文字邊界的字元
\C 或 [:unicode:] 指除了\n以外的所有字元, 和"."保留字作用相同
\Q 及 \E 為了方便設定大量保留字, 可使用 \Q ……\E 的方式, 指定其間的所有字元都是普通字元
== JavaScript 的正則表達式
在 JavaScript 中其正則表達式為一個內建的物件
可用以下方法建立物件
re = new RegExp("pattern", "flag")
re = /pattern/flag //簡寫格式
其中
pattern 是正則表達式的字串。
flag 是比對的方式:g:全域比對(Global match)i:忽略大小寫(Ignore case)
驗證:使用正則式的 test()函數比對字串是否符合規則, 回傳true代表比對正確, 反之回傳false
ex: 身份證字號驗證
<script>
re = /^[A-Z]\d{9}$/;
str = "A123456789";
if (re.test(str))
alert("成功!");
else
alert("失敗!");
</script>
配合 String 的字串功能進行查詢及更換的功能
搜尋符合規則字串的位置(僅能判斷第一個符合的位置)
使用字串的 search()函數搜尋符合規則字串的位置, 若回傳-1代表沒找到
ex:
ex:
<script>
re = /abc/;
str = "123ABC4512abcwwwewwnA123456789n1abc";
alert("字串位置在第 " + str.search(re) + "個字元");
</script>
搜尋所有符合規則的字串資料
使用字串的 match()函數搜尋符合規則字串的字串內容並放置到一個陣列變數內
若未設定全域搜尋, 則只傳回第一個符合的內容值
ex:
<script>
re = /.*abc.*/g;
str = "12345\n12abcww\nweww\nA123456789\n1abc1";
var matchArray = str.match(re);
var matchValue = "";
for(var ii=0; ii<matchArray.length; ii++){
matchValue = matchValue + matchArray[ii] + "<br>";
}
alert("符合的值為 " + matchValue);
</script>
替換所有符合規則的字串資料
使用字串的 replace()函數替換符合規則的字串
若未設定全域搜尋, 則只替換第一個符合的內容值
ex:
<script>
re = /abc/g;
str = "1234567--abc--1212--abc--ABCOK";
var replaceString = str.replace(re,"替換成功");
alert("替換後的值為 " + replaceString);
</script>
語言主要是用來做字串的比對、刪除、替換或拷貝重製等工作。
== 字元分類
字元 – 指ASCII 0~255的所有字元和中文字元(也包中文括符號字元)
數字-- 指0-9的字元(數字是ASCII字元的一部份)
字母 --指英文字母,指 abc….z AB…Z(英文字母是ASCII字元的一部份)
字 -- 字是指被符號、空白隔開的文數字串,指 0-9 a-z A-Z 英文底線以及中文字元(不包括中文字符)
英文字 -- 特別指英文的word,
文數字 -- 指英文字母,中文字元和0-9的數字字元的總稱
白字 -- whitespace
隱藏在資料中空白、換行、跳格(tab)等控制字元, 以”\”加上一個字母來表示如
== 控制字元定義
\t Tab ascii 碼 09($09) , 水平跳格
\n new line feed ascii 碼 10 ($0A) , 跳下一行
\v Vertical tab 碼 11($0B) 垂直跳格
\f form feed ascii 碼 14 ($0C), 換到下一頁
\r carrige return ascii碼15 ($0D), 回車,回到一行的開始
\a Bell 碼 07($07) 鈴聲控制字元.
\e ASCII Escape character 碼 27 ($1B), ESC 鍵
\0 變數名稱, 代表先前的整個 REGEXP 子式的全部內容, 不必被( )所包住
\1 - \9 變數名稱, 代表先前 REGEXP 子式內(…) 所搜到的內容將依序被放入了 \1 \2 … \9
== 保留字元
. ^ $ * + ? { } [ ] \ | ( ) 等為正則表達式的保留字元, 若在搜尋樣版中是要用來表達本來的字元時, 則要在這個字元前
面加上"\"的跳脫符號, 各保留字的定義如下
. 指除了\n以外的所有字元
^ 位於[]內的第一個字元表"非"的意思, 如[^abc] 指除了a或b或c以外的字元
^ 在搜尋使用時表搜尋"開頭"相同的字串
$ 在搜尋使用時表搜尋"結尾"相同的字串
{} 用來定義重複的次數, 如 {n}表n次, {2,5}表2~5次, {,5}表0~5次, {5,}表5到無限次
* 指前一個字元出現 0 或 多次的重複, 即{0,}
+ 指前一個字元至少出現 1 次(1 或 多次的重複), 即{1,}
? 指前一個字元出現 0 或 1 次, 即{0,1}, 如搜尋 colou?r 則 color 及 colour 均會找到
[] 設定字元群組
\ - 為"跳脫字元", 用來指定控制字元時使用
| 或(OR)的意思, 也就是以 | 的兩邊的字串做樣版比對搜尋,只要有一個符合, 就是成功
() 設定"字串樣本"時使用, 其設定的樣本會存放在 \1 ~ \9 的變數內
(?...) 當()號內第一個字元為?時則不是"字串樣本"
== 表達式所使用的符號
[] - 指在[]號內的任一字元符合, 如表達式為 [abc]y 時則, ay, by, cy 等均符合
[^] - 指在[^]號內以外的任一字元符合, 如表達式為 [^ab]y 時則, 除了 ay, by 以外的 ?y 均符合
== 字族名稱
為了簡化表示, 因此定義部份字族名稱來表式某特定的字元群組
\d 或 [:digit:] 指0~9的數字, 就是[0-9]
\u 或 [:upper:] 指大寫英文字母, 就是[A-Z]
\l 或 [:lower:] 指小寫英文字母, 就是[a-z]
\w 或 [:word:] 指所有文數字加上”_”底線, 就是[_A-Za-z_0-9]
\s 或 [:space:] 指白字, 如 [\t\n\r\f] 等
\b 所有英文字的邊界字元, 如 空白字元等
\D 或 [^[:digit:]] 指除了0~9以外的字元, 就是[^0-9]
\U 或 [^[:upper:]] 指除了大寫英文字母以外的字元, 就是[^A-Z]
\L 或 [^[:lower:]] 指除了小寫英文字母以外的字元, 就是[^a-z]
\W 或 [^[:word:]] 指非文字,也就是\w字元族以外的所有字元
\S 或 [^[:space:]] 指非白字,也就是\s以外的字元
\B 所有非英文字邊界的字元
\C 或 [:unicode:] 指除了\n以外的所有字元, 和"."保留字作用相同
\Q 及 \E 為了方便設定大量保留字, 可使用 \Q ……\E 的方式, 指定其間的所有字元都是普通字元
== JavaScript 的正則表達式
在 JavaScript 中其正則表達式為一個內建的物件
可用以下方法建立物件
re = new RegExp("pattern", "flag")
re = /pattern/flag //簡寫格式
其中
pattern 是正則表達式的字串。
flag 是比對的方式:g:全域比對(Global match)i:忽略大小寫(Ignore case)
驗證:使用正則式的 test()函數比對字串是否符合規則, 回傳true代表比對正確, 反之回傳false
ex: 身份證字號驗證
<script>
re = /^[A-Z]\d{9}$/;
str = "A123456789";
if (re.test(str))
alert("成功!");
else
alert("失敗!");
</script>
配合 String 的字串功能進行查詢及更換的功能
搜尋符合規則字串的位置(僅能判斷第一個符合的位置)
使用字串的 search()函數搜尋符合規則字串的位置, 若回傳-1代表沒找到
ex:
ex:
<script>
re = /abc/;
str = "123ABC4512abcwwwewwnA123456789n1abc";
alert("字串位置在第 " + str.search(re) + "個字元");
</script>
搜尋所有符合規則的字串資料
使用字串的 match()函數搜尋符合規則字串的字串內容並放置到一個陣列變數內
若未設定全域搜尋, 則只傳回第一個符合的內容值
ex:
<script>
re = /.*abc.*/g;
str = "12345\n12abcww\nweww\nA123456789\n1abc1";
var matchArray = str.match(re);
var matchValue = "";
for(var ii=0; ii<matchArray.length; ii++){
matchValue = matchValue + matchArray[ii] + "<br>";
}
alert("符合的值為 " + matchValue);
</script>
替換所有符合規則的字串資料
使用字串的 replace()函數替換符合規則的字串
若未設定全域搜尋, 則只替換第一個符合的內容值
ex:
<script>
re = /abc/g;
str = "1234567--abc--1212--abc--ABCOK";
var replaceString = str.replace(re,"替換成功");
alert("替換後的值為 " + replaceString);
</script>
2016年4月11日 星期一
HTML: 瀏覽器的 Cookie 功能
基於安全性考量瀏覽器不能於客戶端的硬碟進行任何存取的動作, 若要存取少量使用者記錄, 則需透過 Cookie 機制來完成
Cookie 是一個存放在 document.cookie 中的字串, 其儲存格式為「name=value;expires=expDate;」
其中 name 代表 Cookie 的名稱,value 則是對應 Cookie 值,expDate 代表 Cookie 的有效期間。
沒指定有效時間,則 Cookie 只會被儲存在記憶體中,使用者關掉瀏覽器或session 逾時後,Cookie 就被自動刪除。
若指定有效時間,則 Cookie 會被儲存在客戶端的硬碟內, 於失效後才會於硬碟中刪除。
== Cookie 操作方式
1. 使用 navigator.cookieEnabled 判斷客戶端的瀏覽器是否開啟 Cookie(true 表開啟)
2. 使用 var x = document.cookie; 可取得完整 Cookie 字串(內容格式為cookie1=value; cookie2=value; cookie3=value; 不包含有效期限資訊)
3. 使用 document.cookie="aaa=bbb" 語句時,是直接將新資料加在 Cookie 字串尾端, 而不會蓋掉原先就有的 Cookie 資料
4. 處理 Cookie 的基本語句為,而 document.cookie="name=value; expires=..."
若設定的名稱(name)不存在原有 Cookie 內, 且有效日期大於當時的時間, 則為新增 Cookie 資料
若設定的名稱(name)已存在原有 Cookie 內, 且有效日期大於當時的時間, 則為修改 Cookie 內該名稱所對應的值
若設定的名稱(name)已存在原有 Cookie 內, 但有效日期小於當時的時間, 則為刪除 Cookie 內該名稱的內容
== Cookie 參考範例
1. 設定 Cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
2. 讀取單一 Cookie 設定值
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
3. 綜合運用(於 Cookie 內讀取使用者名稱, 若不存在則輸入使用者名稱, 並存入 Cookie 內)
function checkCookie() {
var username=getCookie("username");
if (username!="") {
alert("Welcome again " + username);
}else{
username = prompt("Please enter your name:", "");
if (username != "" && username != null) {
setCookie("username", username, 365);
}
}
}
註: Cookie 的設定值內可能會有逗號, 分號或空白等字元, 為避免資料剖析造成錯誤, 可於設定 Cookie 時用 escape() 函數重新編碼, 並於讀取 Cookie 時用 unescape() 函數還原回原有值
Cookie 是一個存放在 document.cookie 中的字串, 其儲存格式為「name=value;expires=expDate;」
其中 name 代表 Cookie 的名稱,value 則是對應 Cookie 值,expDate 代表 Cookie 的有效期間。
沒指定有效時間,則 Cookie 只會被儲存在記憶體中,使用者關掉瀏覽器或session 逾時後,Cookie 就被自動刪除。
若指定有效時間,則 Cookie 會被儲存在客戶端的硬碟內, 於失效後才會於硬碟中刪除。
== Cookie 操作方式
1. 使用 navigator.cookieEnabled 判斷客戶端的瀏覽器是否開啟 Cookie(true 表開啟)
2. 使用 var x = document.cookie; 可取得完整 Cookie 字串(內容格式為cookie1=value; cookie2=value; cookie3=value; 不包含有效期限資訊)
3. 使用 document.cookie="aaa=bbb" 語句時,是直接將新資料加在 Cookie 字串尾端, 而不會蓋掉原先就有的 Cookie 資料
4. 處理 Cookie 的基本語句為,而 document.cookie="name=value; expires=..."
若設定的名稱(name)不存在原有 Cookie 內, 且有效日期大於當時的時間, 則為新增 Cookie 資料
若設定的名稱(name)已存在原有 Cookie 內, 且有效日期大於當時的時間, 則為修改 Cookie 內該名稱所對應的值
若設定的名稱(name)已存在原有 Cookie 內, 但有效日期小於當時的時間, 則為刪除 Cookie 內該名稱的內容
== Cookie 參考範例
1. 設定 Cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
2. 讀取單一 Cookie 設定值
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
3. 綜合運用(於 Cookie 內讀取使用者名稱, 若不存在則輸入使用者名稱, 並存入 Cookie 內)
function checkCookie() {
var username=getCookie("username");
if (username!="") {
alert("Welcome again " + username);
}else{
username = prompt("Please enter your name:", "");
if (username != "" && username != null) {
setCookie("username", username, 365);
}
}
}
註: Cookie 的設定值內可能會有逗號, 分號或空白等字元, 為避免資料剖析造成錯誤, 可於設定 Cookie 時用 escape() 函數重新編碼, 並於讀取 Cookie 時用 unescape() 函數還原回原有值
HTML: 瀏覽器常見的事件(Event)
== 瀏覽器常見的事件
onClick:點擊某一個物件時
onDbclick:雙重點擊某一個物件時
onBlur:失去焦點時
onFocus:得到焦點時
onError:產生錯誤時
onAbort : 圖片下載被中斷時
onSubmit:送出表單資訊時
onReset:表單重設時
onSelect:表單選項被改變時
onChange:改變物件選項或字串時
onScroll:網頁文件被上下捲動時
onResize:調整瀏覽器視窗大小時
onCopy:資料拷貝時
onCut:資料剪下時
onPaste:資料貼上時
onLoad:瀏覽器載入網頁時
onUnload:瀏覽器離開網頁時
onBeforeUnload:瀏覽器離開網頁前的時候
== 如何設定啟用 onLoad 及 onUnload 事件
使用 <body onLoad="alert(str1)" onUnload="alert(str2)">
或使用 window.onLoad=func1; 和window.onUnload=func2;
註:由於 onUnload 事件是在瀏覽器離開網頁時觸發, 由於事件傳遞可能會有時間差, 所以 onUnload 事件的處理程序不一定能完整執行完畢, 若要保證該段程式碼必須於視窗關閉執行完畢, 則需使用 onBeforeUnload 事件函式處理
== 處理鍵盤事件
onkeyDown:在鍵盤上按下任何一個按鍵時觸發(包含單獨按下 Shift, Ctrl, Alt, Tab... 等按鍵)
onkeyUp:鍵盤上的按鍵釋放時觸發
onkeyPress:於keyDown及keyUp均會檢查, 若當時的鍵盤可取得一個正確的按鍵值時就會觸發 onkeyPress 事件(Shift, Ctrl 等需為組合鍵時才會觸發)
使用方式
1. 指定處理鍵盤事件的函式
使用 <body onkeydown="keyDown(event)"> (需傳入 event 物件)
或使用 document.onkeydown=keyDown; (只要設定函數名稱, 在函式內直接使用 event 物件)
2. 在函式內讀取 event 物件內的相關參數值可得知當時鍵盤的按鍵狀況
event.keyCode 的值在 onkeyDown 及 onkeyPress 事件時會傳回不同的值
在 onkeyDown 事件中, event.keyCode 為按鍵代碼, 而在在 onkeyPress 事件中, event.keyCode 的傳回值則為按鍵值的ASCII CODE
如 B 的按鍵代碼為 66, 在onkeyDown事件中其傳回值永遠是66, 但在onkeyPress事件時則會依大小寫不同而傳回不同的值(B為66, b為98)
另如 , 和 < 是同一個按鍵, 其按鍵代碼為 188, 在onkeyDown事件中其傳回值永遠是188, 但在onkeyPress事件時若單獨按下該鍵會是,號的ASCII(44), 若shift 也同時按下則會<號的ASCII(60)
依大小寫不同而傳回不同的值(B為66, b為98)
event.shiftKey, event.ctrlKey, event.altKey 等值, 可研判 Shift, Ctrl 及 Alt 等按鍵的狀態(true 表按鍵被按下)
== 處理滑鼠事件
onMouseDown:按下滑鼠按鈕時
onMouseUp:鬆開滑鼠按鈕時
onMouseMove:滑鼠移動時
onMouseOver:滑鼠移動進入某個物件時
onMouseOut:滑鼠移動離開某個物件時
onMouseWheel:滑鼠滾動滾輪時
onDrag:滑鼠「拖」動時
onDrop:滑鼠「放」下時
onClick:滑鼠點擊時
onDbclick:滑鼠雙重點時
與滑鼠事件相關的屬性值
event.button : 取得哪一個滑鼠按鍵被按下
W3C標準 -> 0:左鍵, 1:中鍵, 2:右鍵
IE瀏覽器 -> 1:左鍵, 4:中鍵, 2:右鍵
event.x, event.y, event.clientX, event.clientY : 滑鼠相對於視窗位置的x與y座標值;
event.offsetX, event.offsetY : 滑鼠與發送事件之物件的其相對位置x與y之座標值;
== 其他事件的屬性值
event.srcElement : 產生事件的物件或元素
event.type : 事件類別
== 限制瀏覽器部份功能的方法
<body
onMouseover="window.status='';return true;" (清空狀態列內容 - 不顯示狀態列)
onDragStart="window.event.returnValue=false" (取消拖曳功能)
onContextMenu="window.event.returnValue=false" (取消按右鍵出現選單的功能)
onSelectStart="event.returnValue=false"> (取消全選功能)
onClick:點擊某一個物件時
onDbclick:雙重點擊某一個物件時
onBlur:失去焦點時
onFocus:得到焦點時
onError:產生錯誤時
onAbort : 圖片下載被中斷時
onSubmit:送出表單資訊時
onReset:表單重設時
onSelect:表單選項被改變時
onChange:改變物件選項或字串時
onScroll:網頁文件被上下捲動時
onResize:調整瀏覽器視窗大小時
onCopy:資料拷貝時
onCut:資料剪下時
onPaste:資料貼上時
onLoad:瀏覽器載入網頁時
onUnload:瀏覽器離開網頁時
onBeforeUnload:瀏覽器離開網頁前的時候
== 如何設定啟用 onLoad 及 onUnload 事件
使用 <body onLoad="alert(str1)" onUnload="alert(str2)">
或使用 window.onLoad=func1; 和window.onUnload=func2;
註:由於 onUnload 事件是在瀏覽器離開網頁時觸發, 由於事件傳遞可能會有時間差, 所以 onUnload 事件的處理程序不一定能完整執行完畢, 若要保證該段程式碼必須於視窗關閉執行完畢, 則需使用 onBeforeUnload 事件函式處理
== 處理鍵盤事件
onkeyDown:在鍵盤上按下任何一個按鍵時觸發(包含單獨按下 Shift, Ctrl, Alt, Tab... 等按鍵)
onkeyUp:鍵盤上的按鍵釋放時觸發
onkeyPress:於keyDown及keyUp均會檢查, 若當時的鍵盤可取得一個正確的按鍵值時就會觸發 onkeyPress 事件(Shift, Ctrl 等需為組合鍵時才會觸發)
使用方式
1. 指定處理鍵盤事件的函式
使用 <body onkeydown="keyDown(event)"> (需傳入 event 物件)
或使用 document.onkeydown=keyDown; (只要設定函數名稱, 在函式內直接使用 event 物件)
2. 在函式內讀取 event 物件內的相關參數值可得知當時鍵盤的按鍵狀況
event.keyCode 的值在 onkeyDown 及 onkeyPress 事件時會傳回不同的值
在 onkeyDown 事件中, event.keyCode 為按鍵代碼, 而在在 onkeyPress 事件中, event.keyCode 的傳回值則為按鍵值的ASCII CODE
如 B 的按鍵代碼為 66, 在onkeyDown事件中其傳回值永遠是66, 但在onkeyPress事件時則會依大小寫不同而傳回不同的值(B為66, b為98)
另如 , 和 < 是同一個按鍵, 其按鍵代碼為 188, 在onkeyDown事件中其傳回值永遠是188, 但在onkeyPress事件時若單獨按下該鍵會是,號的ASCII(44), 若shift 也同時按下則會<號的ASCII(60)
依大小寫不同而傳回不同的值(B為66, b為98)
event.shiftKey, event.ctrlKey, event.altKey 等值, 可研判 Shift, Ctrl 及 Alt 等按鍵的狀態(true 表按鍵被按下)
== 處理滑鼠事件
onMouseDown:按下滑鼠按鈕時
onMouseUp:鬆開滑鼠按鈕時
onMouseMove:滑鼠移動時
onMouseOver:滑鼠移動進入某個物件時
onMouseOut:滑鼠移動離開某個物件時
onMouseWheel:滑鼠滾動滾輪時
onDrag:滑鼠「拖」動時
onDrop:滑鼠「放」下時
onClick:滑鼠點擊時
onDbclick:滑鼠雙重點時
與滑鼠事件相關的屬性值
event.button : 取得哪一個滑鼠按鍵被按下
W3C標準 -> 0:左鍵, 1:中鍵, 2:右鍵
IE瀏覽器 -> 1:左鍵, 4:中鍵, 2:右鍵
event.x, event.y, event.clientX, event.clientY : 滑鼠相對於視窗位置的x與y座標值;
event.offsetX, event.offsetY : 滑鼠與發送事件之物件的其相對位置x與y之座標值;
== 其他事件的屬性值
event.srcElement : 產生事件的物件或元素
event.type : 事件類別
== 限制瀏覽器部份功能的方法
<body
onMouseover="window.status='';return true;" (清空狀態列內容 - 不顯示狀態列)
onDragStart="window.event.returnValue=false" (取消拖曳功能)
onContextMenu="window.event.returnValue=false" (取消按右鍵出現選單的功能)
onSelectStart="event.returnValue=false"> (取消全選功能)
2016年4月8日 星期五
HTML: DOM 的物件模型
DOM 物件的屬性與方法
document.activeElement Returns the currently focused element in the document
document.addEventListener() Attaches an event handler to the document
document.adoptNode() Adopts a node from another document
document.anchors Returns a collection of all <a> elements in the document that have a name attribute
document.applets Returns a collection of all <applet> elements in the document
document.baseURI Returns the absolute base URI of a document
document.body Sets or returns the document's body (the <body> element)
document.close() Closes the output stream previously opened with document.open()
document.cookie Returns all name/value pairs of cookies in the document
document.createAttribute() Creates an attribute node
document.createComment() Creates a Comment node with the specified text
document.createDocumentFragment() Creates an empty DocumentFragment node
document.createElement() Creates an Element node
document.createTextNode() Creates a Text node
document.doctype Returns the Document Type Declaration associated with the document
document.documentElement Returns the Document Element of the document (the <html> element)
document.documentMode Returns the mode used by the browser to render the document
document.documentURI Sets or returns the location of the document
document.domain Returns the domain name of the server that loaded the document
document.domConfig Obsolete. Returns the DOM configuration of the document
document.embeds Returns a collection of all <embed> elements the document
document.forms Returns a collection of all <form> elements in the document
document.getElementById() Returns the element that has the ID attribute with the specified value
document.getElementsByClassName() Returns a NodeList containing all elements with the specified class name
document.getElementsByName() Returns a NodeList containing all elements with a specified name
document.getElementsByTagName() Returns a NodeList containing all elements with the specified tag name
document.hasFocus() Returns a Boolean value indicating whether the document has focus
document.head Returns the <head> element of the document
document.images Returns a collection of all <img> elements in the document
document.implementation Returns the DOMImplementation object that handles this document
document.importNode() Imports a node from another document
document.inputEncoding Returns the encoding, character set, used for the document
document.lastModified Returns the date and time the document was last modified
document.links Returns a collection of all <a> and <area> elements in the document that have a href attribute
document.normalize() Removes empty Text nodes, and joins adjacent nodes
document.normalizeDocument() Removes empty Text nodes, and joins adjacent nodes
document.open() Opens an HTML output stream to collect output from document.write()
document.querySelector() Returns the first element that matches a specified CSS selector(s) in the document
document.querySelectorAll() Returns a static NodeList containing all elements that matches a specified CSS selector(s) in the document
document.readyState Returns the (loading) status of the document
document.referrer Returns the URL of the document that loaded the current document
document.removeEventListener() Removes an event handler from the document (that has been attached with the addEventListener() method)
document.renameNode() Renames the specified node
document.scripts Returns a collection of <script> elements in the document
document.strictErrorChecking Sets or returns whether error-checking is enforced or not
document.title Sets or returns the title of the document
document.URL Returns the full URL of the HTML document
document.write() Writes HTML expressions or JavaScript code to a document
document.writeln() Same as write(), but adds a newline character after each statement
========
HTML DOM Properties
x.innerHTML - the text value of x
x.nodeName - the name of x
x.nodeValue - the value of x
x.parentNode - the parent node of x
x.childNodes - the child nodes of x
x.attributes - the attributes nodes of x
Note: In the list above, x is a node object (HTML element).
HTML DOM Methods
x.getElementById(id) - get the element with a specified id
x.getElementsByTagName(name) - get all elements with a specified tag name
x.appendChild(node) - insert a child node to x
x.removeChild(node) - remove a child node from x
Note: In the list above, x is a node object (HTML element).
document.activeElement Returns the currently focused element in the document
document.addEventListener() Attaches an event handler to the document
document.adoptNode() Adopts a node from another document
document.anchors Returns a collection of all <a> elements in the document that have a name attribute
document.applets Returns a collection of all <applet> elements in the document
document.baseURI Returns the absolute base URI of a document
document.body Sets or returns the document's body (the <body> element)
document.close() Closes the output stream previously opened with document.open()
document.cookie Returns all name/value pairs of cookies in the document
document.createAttribute() Creates an attribute node
document.createComment() Creates a Comment node with the specified text
document.createDocumentFragment() Creates an empty DocumentFragment node
document.createElement() Creates an Element node
document.createTextNode() Creates a Text node
document.doctype Returns the Document Type Declaration associated with the document
document.documentElement Returns the Document Element of the document (the <html> element)
document.documentMode Returns the mode used by the browser to render the document
document.documentURI Sets or returns the location of the document
document.domain Returns the domain name of the server that loaded the document
document.domConfig Obsolete. Returns the DOM configuration of the document
document.embeds Returns a collection of all <embed> elements the document
document.forms Returns a collection of all <form> elements in the document
document.getElementById() Returns the element that has the ID attribute with the specified value
document.getElementsByClassName() Returns a NodeList containing all elements with the specified class name
document.getElementsByName() Returns a NodeList containing all elements with a specified name
document.getElementsByTagName() Returns a NodeList containing all elements with the specified tag name
document.hasFocus() Returns a Boolean value indicating whether the document has focus
document.head Returns the <head> element of the document
document.images Returns a collection of all <img> elements in the document
document.implementation Returns the DOMImplementation object that handles this document
document.importNode() Imports a node from another document
document.inputEncoding Returns the encoding, character set, used for the document
document.lastModified Returns the date and time the document was last modified
document.links Returns a collection of all <a> and <area> elements in the document that have a href attribute
document.normalize() Removes empty Text nodes, and joins adjacent nodes
document.normalizeDocument() Removes empty Text nodes, and joins adjacent nodes
document.open() Opens an HTML output stream to collect output from document.write()
document.querySelector() Returns the first element that matches a specified CSS selector(s) in the document
document.querySelectorAll() Returns a static NodeList containing all elements that matches a specified CSS selector(s) in the document
document.readyState Returns the (loading) status of the document
document.referrer Returns the URL of the document that loaded the current document
document.removeEventListener() Removes an event handler from the document (that has been attached with the addEventListener() method)
document.renameNode() Renames the specified node
document.scripts Returns a collection of <script> elements in the document
document.strictErrorChecking Sets or returns whether error-checking is enforced or not
document.title Sets or returns the title of the document
document.URL Returns the full URL of the HTML document
document.write() Writes HTML expressions or JavaScript code to a document
document.writeln() Same as write(), but adds a newline character after each statement
========
HTML DOM Properties
x.innerHTML - the text value of x
x.nodeName - the name of x
x.nodeValue - the value of x
x.parentNode - the parent node of x
x.childNodes - the child nodes of x
x.attributes - the attributes nodes of x
Note: In the list above, x is a node object (HTML element).
HTML DOM Methods
x.getElementById(id) - get the element with a specified id
x.getElementsByTagName(name) - get all elements with a specified tag name
x.appendChild(node) - insert a child node to x
x.removeChild(node) - remove a child node from x
Note: In the list above, x is a node object (HTML element).
JS: JavaScript 自定物件
========
建立單一物件
student = new Object();
student.name = "David";
student.age = "25";
student.phone = "2645-1234";
使用 new Object() 產生一個使用者自訂物件
於物件內建立三個屬性值
========
定義物件結構, 產生多個相同結構的物件
// 建立物件結構
function book(bookName, price) {
this.name = bookName;
this.price = price;
this.info = showInfo;
}
function showInfo() {
var outStr="BookName = "+this.name+"\n";
outStr = outStr + "BookPrice = "+this.price;
alert(outStr);
}
// 使用 new book() 產生新的 book 物件
book1 = new book("JavaScript 程式設計",460);
book2 = new book("JavaScript 簡介",320);
// 使用 book1.info 可顯示 book1 的資訊
book1.info
建立單一物件
student = new Object();
student.name = "David";
student.age = "25";
student.phone = "2645-1234";
使用 new Object() 產生一個使用者自訂物件
於物件內建立三個屬性值
========
定義物件結構, 產生多個相同結構的物件
// 建立物件結構
function book(bookName, price) {
this.name = bookName;
this.price = price;
this.info = showInfo;
}
function showInfo() {
var outStr="BookName = "+this.name+"\n";
outStr = outStr + "BookPrice = "+this.price;
alert(outStr);
}
// 使用 new book() 產生新的 book 物件
book1 = new book("JavaScript 程式設計",460);
book2 = new book("JavaScript 簡介",320);
// 使用 book1.info 可顯示 book1 的資訊
book1.info
訂閱:
文章 (Atom)
