端訓練營:1v1私教,終身輔導計劃,幫你拿到滿意的 offer。 已幫助數百位同學拿到了中大廠 offer。歡迎來撩~~~~~~~~
Hello,大家好,我是 Sunday。
說起 HTML 很多同學都會認為,這不就是小菜一碟嗎?這玩意有啥難度?你也太瞧不起我了吧!
不過看似越簡單的東西,我們越會忽略,同時它們還可以提供出令人驚艷的效果!
過去,我們經常使用本機瀏覽器窗口作為彈出框元素來顯示頂層內容(例如:常見的 dialog)。
可是這些默認的彈窗并不友好。所以后來,我們使用外部庫(Element、AntD 等)構建了頁面內的、經過CSS樣式化的彈出框,以好看的UI方式方式顯示此類內容。
不過現在,你可以使用純HTML創建彈出框,而無需編寫任何JavaScript代碼。現代Web標準通過Popover API提供了內置的彈出框支持。
看下面的源代碼:
<style>
div[popover]::backdrop {
background-color: rgba(0, 0, 0, 0.4);
}
div[popover] {
padding: 12px;
border: none;
}
</style>
<div id="pop" popover>
我是彈出的內容
</div>
<button popovertarget="pop">展示 popover</button>
上述HTML使用了 popover 屬性來實現一個簡單的彈出框元素。
它使用 popovertarget 屬性來在不使用JavaScript的情況下顯示彈出框元素。此外,我們使用了 ::backdrop 偽元素來樣式化彈出框的背景:
你可以使用 @starting-style 為原生彈出框添加動畫,正如 MDN 文檔(https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-style)中所解釋的那樣。
HTML標準提供了 autofocus 屬性,用于在頁面加載和對話框/彈出框顯示狀態下自動聚焦表單元素。
看下面的示例模態框,它在第一個輸入已經預填充的情況下自動將焦點設置到第二個文本輸入框上:
<dialog id="dlg">
<form method="dialog">
<input type="text" placeholder="Firstname" value="張" />
<div style="height: 8px"></div>
<input type="text" placeholder="Lastname" autofocus />
<div style="height: 8px"></div>
<button>保存</button>
</form>
</dialog>
<button onclick="document.getElementById('dlg').showModal()" autofocus>點我</button>
上面的HTML在兩個地方使用了 autofocus 屬性:
此外,我們還使用了 method="dialog" 屬性來設置關閉對話框而無需使用JavaScript代碼。
表單校驗是日常開發的常見需求。不過當我們脫離了 Element、AntD 這些組件庫之后,你還知道如何實現表單校驗嗎?
其實對于 HTML的 input 元素來說,它是有 pattern 屬性的
假設我們需要驗證一個產品標識符,它包含兩個英文字母和六個數字,用連字符連接,即 GR-100200。
以下HTML片段實現了上述要求的驗證功能的文本輸入框:
<form>
<label for="productID"> ID:</label>
<input type="text" id="productID" name="productID" pattern="[A-Za-z]{2}-\d{6}" title="Please enter a valid product identifier (e.g., GR-100200)" required>
<button type="submit">Submit</button>
</form>
在上面的示例中,我們使用 pattern 屬性設置了一個正則表達式,用于驗證產品標識符。此正則表達式要求兩個英文字母(不區分大小寫)后跟一個連字符,然后是六個數字。另外,我們還添加了 title 屬性,以提供關于輸入格式的說明。最后,我們將 required 屬性添加到輸入元素上,以確保用戶輸入有效的產品標識符。
這里,pattern 屬性通過顯示瀏覽器特定的驗證消息來阻止表單提交無效輸入。但是他必須要在點擊了 提交 按鈕之后才可以進行校驗。
如果我們想要進行實時校驗(根據輸入內容實時校驗)怎么辦呢?
我們可以使用 :valid 和 :invalid CSS 偽類來實現 pattern 的實時驗證,如下所示的 HTML 代碼片段所示:
<style>
input[type=text] {
border: #000 1px solid;
border-radius: 4px;
outline: none;
padding: 6px;
}
input[type=text]:invalid {
border: red 1px solid;
+span::before {
content: '?';
display: inline;
color: red;
}
}
input[type=text]:valid {
border: green 1px solid;
+span::before {
content: '?';
display: inline;
color: green;
}
}
</style>
<input type="text" placeholder="i.e., GR-100200" pattern="[A-Z]{2}-[0-9]{6}" required />
<span></span>
上面的HTML片段使用CSS代碼根據驗證狀態設置樣式。無效輸入會將輸入框邊框設置為紅色,并顯示紅色的叉號。與此同時,有效輸入會呈現綠色邊框和綠色的勾號符號:
手機通過虛擬鍵盤進行輸入,這個鍵盤有幾種模式。
例如,它可能僅顯示數字鍵用于數字輸入元素,對于一般的字符串輸入則顯示完整的鍵盤界面。移動瀏覽器會根據輸入類型自動更改虛擬鍵盤模式,但開發人員也可以使用 input 元素的 inputmode 屬性進行自定義。
<input type="text" pattern="[0-9]{6}" inputmode="numeric" maxlength="6">
在上面的示例中,我們使用了 inputmode 屬性來指定虛擬鍵盤的模式為 numeric,以便在移動設備上只顯示數字鍵盤。同時,我們還使用了 pattern 屬性來限制輸入只能是六位數字。maxlength 屬性限制輸入的最大長度為六位。
圖片懶加載是日常開發中的常見需求。我們在實現懶加載時大多數會使用一些現成的庫或者基于 Intersection Observer API 進行處理
不過很多同學不知道的是:img 標簽的 loading 屬性可以讓你在不編寫JavaScript代碼或使用第三方庫的情況下啟用瀏覽器級別的圖片懶加載。
看下面的源代碼:
<div style="height: 2000px"></div>
<img src="https://gips3.baidu.com/it/u=45328832,131546734&fm=3039&app=3039&f=JPEG?w=1024&h=1024
" loading="lazy" />
以這種方式實現圖片懶加載的功能,當頁面往下滾動的時候,圖片動態加載:
HTML 5 也被稱為 Web Applications 1.0。為了實現這個目標,增加了幾個為 Web 頁面提供交互體驗的新元素:
details
datagrid
menu
command
這些元素都可以根據用戶的操作和選擇改變顯示的內容,而不需要從服務器裝載新頁面。
details
details 元素表示在默認情況下可能不顯示的詳細信息。可選的 legend 元素可以提供詳細信息的摘要。
details 元素的用途之一是提供腳注和尾注。例如:
The bill of a Craveri's Murrelet is about 10% thinner
than the bill of a Xantus's Murrelet.
<details>
<legend>[Sibley, 2000]</legend>
<p>Sibley, David Allen, The Sibley Guide to Birds,
(New York: Chanticleer Press, 2000) p. 247
</p>
</details>
沒有指定具體的顯示方式。瀏覽器可以選用腳注、尾注和工具提示等方式。
每個 details 元素可以有一個 open 屬性。如果設置了這個屬性,那么詳細信息在最初就顯示出來。如果沒有設置這個屬性,那么會隱藏它們,直到用戶要求顯示它們。無論是哪種情況,用戶都可以通過單擊一個圖標或其他控件來顯示或隱藏詳細信息。
datagrid
datagrid 元素提供一個網格控件。可以用它顯示樹、列表和表格,用戶和腳本可以更新這些界面元素。與之相反,傳統的表格主要用來顯示靜態數據。
datagrid 從它的內容(一個 table、select 或其他 HTML 元素)獲得初始數據。例如,代碼 9 中的 datagrid 包含一張成績表。在這個示例中,datagrid 的數據來自一個 table。更簡單的一維 datagrid 可以從 select 元素獲得數據。如果使用其他 HTML 元素,那么每個子元素成為網格中的一行。
<datagrid>
<table>
<tr><td>Jones</td><td>Allison</td><td>A-</td><td>B </td><td>A</td></tr>
<tr><td>Smith</td><td>Johnny</td><td>A</td><td>C </td><td>A</td></tr>
<tr><td>Willis</td><td>Sydney</td><td>C-</td><td>D</td><td>F</td></tr>
<tr><td>Wilson</td><td>Frank</td><td>B-</td><td>B </td><td>A</td></tr>
</table>
</datagrid>
這個元素與常規表格的區別在于,用戶可以選擇行、列和單元格;把行、列和單元格折疊起來;編輯單元格;刪除行、列和單元格;對網格排序;以及在客戶機瀏覽器中直接進行其他數據操作。可以用 JavaScript 代碼監視更新。Document Object Model(DOM)中增加了 HTMLDataGridElement 接口以支持這個元素(代碼 10 HTMLDataGridElement)。
interface HTMLDataGridElement : HTMLElement {
attribute DataGridDataProvider data;
readonly attribute DataGridSelection selection;
attribute boolean multiple;
attribute boolean disabled;
void updateEverything();
void updateRowsChanged(in RowSpecification row, in unsigned long count);
void updateRowsInserted(in RowSpecification row, in unsigned long count);
void updateRowsRemoved(in RowSpecification row, in unsigned long count);
void updateRowChanged(in RowSpecification row);
void updateColumnChanged(in unsigned long column);
void updateCellChanged(in RowSpecification row, in unsigned long column);
};
還可以使用 DOM 在網格中動態地裝載數據。也就是說,datagrid 可以不包含那些提供初始數據的子元素。可以用一個 DataGridDataProvider 對象設置它(代碼 11 DataGridDataProvider)。這樣就可以從數據庫、XmlHttpRequest 或者 JavaScript 代碼能夠訪問的任何資源裝載數據。
interface DataGridDataProvider {
void initialize(in HTMLDataGridElement datagrid);
unsigned long getRowCount(in RowSpecification row);
unsigned long getChildAtPosition(in RowSpecification parentRow,
in unsigned long position);
unsigned long getColumnCount();
DOMString getCaptionText(in unsigned long column);
void getCaptionClasses(in unsigned long column, in DOMTokenList classes);
DOMString getRowImage(in RowSpecification row);
HTMLMenuElement getRowMenu(in RowSpecification row);
void getRowClasses(in RowSpecification row, in DOMTokenList classes);
DOMString getCellData(in RowSpecification row, in unsigned long column);
void getCellClasses(in RowSpecification row, in unsigned long column,
in DOMTokenList classes);
void toggleColumnSortState(in unsigned long column);
void setCellCheckedState(in RowSpecification row, in unsigned long column,
in long state);void cycleCell(in RowSpecification row, in unsigned long column);
void editCell(in RowSpecification row, in unsigned long column, in DOMString data);
};
menu 和 command
menu 元素實際上在 HTML 2 中就出現了。在 HTML 4 中廢棄了它,但是 HTML 5 又恢復了它并指定了新的意義。在 HTML 5 中,menu 包含 command 元素,每個 command 元素引發一個操作。例如,代碼 12 HTML 5 菜單 是一個彈出警告框的菜單。
<menu>
<commandlabel="Do 1st Command"/>
<command label="Do 2nd Command"/>
<commandlabel="Do 3rd Command"/>
</menu>
還可以用 checked="checked" 屬性將命令轉換為復選框。通過指定 radiogroup 屬性,可以將復選框轉換為單選按鈕,這個屬性的值是互相排斥的按鈕的組名。
除了簡單的命令列表之外,還可以使用 menu 元素創建工具欄或彈出式上下文菜單,這需要將 type 屬性設置為 toolbar 或 popup。例如,代碼 13. HTML 5 工具欄 顯示一個與 WordPress 等 blog 編輯器相似的工具欄。它使用 icon 屬性鏈接到按鈕的圖片。
<menu type="toolbar">
<commandlabel="strong" icon="bold.gif"/>
<command onclick="insertTag(buttons, 1);"label="em" icon="italic.gif"/>
<command onclick="insertLink(buttons, 2);" label="link" icon="link.gif"/>
<commandlabel="b-quote" icon="blockquote.gif"/>
<command onclick="insertTag(buttons, 4);"label="del" icon="del.gif"/>
<command onclick="insertTag(buttons, 5);"label="ins" icon="insert.gif"/>
<command label="img" icon="image.gif"/>
<commandlabel="ul" icon="bullet.gif"/>
<commandlabel="ol" icon="number.gif"/>
<commandlabel="li" icon="item.gif"/>
<command label="code" icon="code.gif"/>
<command onclick="insertTag(buttons, 11);" label="cite" icon="cite.gif"/>
<command label="abbr" icon="abbr.gif"/>
<command label="acronym" icon="acronym.gif"/>
</menu>
label 屬性提供菜單的標題。例如,代碼14. HTML 5 Edit 菜單 顯示一個 Edit 菜單。
<menu type="popup" label="edit">
<command label="Undo"/>
<command label="Redo"/>
<commandlabel="Cut"/>
<command onclick="copy()" label="Copy"/>
<command onclick="paste()"label="Paste"/>
<command label="Clear"/>
</menu>
菜單可以嵌套在其他菜單中,形成層次化的菜單結構。
終效果如下圖:
圖0
一、javascript可以寫在body標簽里面,這里用的是alert,它的作用就是彈出一個消息框。
圖1
*請認真填寫需求信息,我們會在24小時內與您取得聯系。