如何在 HTML 中指定某個元素尚不相關?
布林屬性構成了hidden 屬性。
它表示當存在時,元素要麼尚不重要,要麼不再相關。指定了 hidden 屬性的元素不應由瀏覽器顯示。hidden 屬性的另一個應用是阻止使用者檢視元素,直到滿足不同的要求。
語法
<p hidden> ……… </p>
以下是示例……
示例
我們使用 hidden 屬性隱藏 html 指令碼中不相關的元素。
<!DOCTYPE html> <html> <body> <h2>Heading</h2> <p>This is a demo paragraph and visible.</p> <p hidden>This is a demo paragraph and hidden.</p> </body> </html>
輸出
執行上述指令碼後獲得的輸出為 -
示例
在以下示例中,我們使用 hidden 屬性來指定元素不相關。
<!DOCTYPE html> <html> <body> <p hidden>A laptop, laptop computer, or notebook computer is a small, portable personal computer with a screen and alphanumeric keyboard.</p> <p>A laptop is a personal computer that can be easily moved and used in a variety of locations.</p> </body> </html>
輸出
執行上述指令碼後,隱藏內容中的文字將顯示,其餘文字也將顯示。
示例:隱藏表單中的內容
可以使用 hidden 屬性隱藏表單中的不相關資訊。但是,語法略有不同 -
語法
<input type = “hidden”>
讓我們看一個建立的求職申請表單,其中有三個輸入空間:輸入姓名、招聘經理提出的問題和薪資條款。但是,薪資條款被隱藏,因為它與求職申請表單無關。
<form> <div> <label for="title">Enter Name:</label> <input type="text" id="title" name="title" value="My Name"> </div> <div> <label for="content">Why should we hire you? Write in 300 words:</label> <textarea id="content" name="content" cols="60" rows="5"> Some Content Here. </textarea> </div> <div> <label for="text"> Salary (Is hidden):</label> <input type="hidden" id="text" name="text" value="Hidden Content"> </div> <div> <button type="submit">Update post</button> </div> <input type="hidden" id="postId" name="postId" value="34657"> </form>
輸出
執行上述指令碼後獲得的輸出為:
廣告