Detailed introduction to the new attributes of HTML5 forms
This article mainly introduces the new attributes in HTML5form in detail, and attaches an example analysis, which is simple and easy to understand. Friends who need it, let’s take a look
1. The form attribute of the element in the form
In H5, you can put the form on the page anywhere, and then specify a form attribute for the element, the attribute value is the id of the form, so that you can declare that the element is subordinate to the specified form
<form id="testform"> <input type="text"> </form> <textarea form="testform"></textarea>
2. The formaction attribute of the elements in the form
In H4, the form submission method
<form action="1.jsp"> </form>
Submit the entire form
In H5, you can add the jsp page submitted to each attribute of the form
<form action="1.jsp"> <input type="submit" action="1-1.jsp">提交到1页面</form> <input type="submit" action="1-2.jsp">提交到2页面</form> <input type="submit" action="1-3.jsp">提交到3页面</form> </form>
3. The formmethod attribute of the element in the form
You can specify the submission method for each element of the form
<form action="1.jsp"> <input type="submit" action="1-1.jsp" formmethod="post">提交到1页面,post提交方法</form> <input type="submit" action="1-2.jsp" formmethod="get">提交到2页面,get提交方法</form> </form>
4. The formenctype attribute of the elements in the form
Same as above, the effect is on each element, the code is abbreviated
5. The formtarget attribute of the form
The same as above, the function is on every element, the code is abbreviated
6. The autofocus attribute of the element
Add the autofocus attribute to the text box, selection box and button control , etc. When the screen is opened, it will Automatically gets focus.
<input type="text" action="1-3.jsp" autofocus>提交到3页面</form>
Only one control on a page can have the autofocus attribute and cannot be abused
7. required attribute
can be used in most input elements. If the input value is empty, submission is not allowed. The browser displays text information to prompt the user that the content must be entered.
8. labels attribute
You can add a labels attribute to all elements of the form. The attribute value is a NodeList object, which represents the label elements bound to the element. collection.
<script type="text/javascript"> window.onload = function () { var text = document.getElementById('text'); var btn = document.getElementById('btn'); var form = document.getElementById('form'); btn.onclick = function() { if (text.value.trim() =="") { //alert(text.labels.length) if(text.labels.length==1) { var label = document.createElement("label"); label.setAttribute("for","text"); form.insertBefore(label,btn); text.labels[1].innerHTML = "请输入姓名"; text.labels[1].setAttribute( "style","font-size:9px;color:red"); } } else if(text.labels.length>1) form.removeChild(text.labels[1]); } } </script> <form id="form"> <label id="label" for="text">姓名:</label> <input id="text"> <input id="btn" type="button" value="验证"> </form>
When the user does not enter any value and clicks Verify, a label element will be dynamically added next to the text box. The label element is "Please enter a name"; when the user When there is input in the text box, the label with the text "Please enter your name" will be deleted.
9. The control attribute of the label
You can place a form element inside the label (label) and access the form element through the control attribute of the label.
<script type="text/javascript"> function setValue() { console.log(1); var label =document.getElementById("label"); var textbox = label.control; textbox.value = "231456" } </script> <form> <label id="label"> 邮编: <input id="txt_zip" maxlength="6"> <small>请输入6位数字</small> </label> <input type="button" value="设置默认值" onclick="setValue()"> </form>
Open the page in the browser, click the "Set Default Value" button, and 231456.
10 will be displayed in the text box. The placeholder attribute of the text box
When the text box (text or textarea) is in the selected state, enter the prompt message (gray)
<input type="text" palceholder="hahah">
11. The list attribute of the text box
Combined with the new datalist element of h5, create a prompt box
text:<input type="text" name="greeting" list="greetings"> <!-- 使用style="display:none";将datalist元素设置为不显示 --> <datalist id="greetings" style="display:none"> <option value="你是人">你是人</option> <option value="你是猪">你是猪</option> <option value="你是狗">你是狗</option> </datalist>
When you are in the text box When you enter "you" in , three columns of prompts "You are a human", "You are a pig", and "You are a dog" will appear below. If there are no keywords, no prompt will appear
12. Text The autocomplete attribute of the box
autocomplete has three values: "on", "off", and "" (not specified). If not specified, the browser's default value is used. The usage method is as follows
<input type="text" name="greeting" list="greetings" autocomplete="on">
When you enter "I am a human" in the text box and click submit, return to the previous page and enter "I" in the text box , the prompt "I am a human" will appear.
13. The pattern attribute of the text box
is equivalent to using regular expression directly in the html part to determine whether the value input meets the requirements.
Please enter the content:
This code requires entering a number of three Capital letters, if entered incorrectly, the prompt "Please be consistent with what was requested (Google)" will appear.
14. The selectionDirection attribute of the text box
For the text and textarea elements of input, you can use it when the user selects part of the text with the mouse in these two elements. This element is used to obtain the selection direction.
<script type="text/javascript"> function alertSelectionDirection() { var control = document.forms[0]['text']; var Direction = control.selectionDirection; alert(Direction); } </script> <form> <input type="text" name="text"> <input type="button" value="惦记我" onclick="alertSelectionDirection()" > </form>
15. The indeterminate attribute of the check box
For the check box, in the past There are only selected and non-selected states. In HTML5, you can use the indeterminate attribute on this element in js to indicate the "not explicitly selected" state of the check box.
<script> var cb = document.getElementById("cb"); //将indeterminate的属性设置为true cb.indeterminate = true; </script> <input type="checkbox" indeterminate id="cb">indeterminate属性测试
16. The height property and the width property
## of the image submit button- #height: Specify the height of the picture in the picture button;
width:指定图片按钮中图片的宽度;
<input type="image" src="img/2.png" width="23" height="23">
17. textarea元素的maxlength属性与wrap属性
maxlength :使用整数值进行设定,用于限定textarea元素中可输入文字的个数。
wrap:可指定属性值为soft与hard.当属性为hard时,在没有用回车键而是文字超出一排规定范围而自动换行时,提交表单时会在换行处添加一个换行标志。
The above is the detailed content of Detailed introduction to the new attributes of HTML5 forms. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When using HTML5SSE, the methods to deal with reconnection and errors include: 1. Understand the default reconnection mechanism. EventSource retrys 3 seconds after the connection is interrupted by default. You can customize the interval through the retry field; 2. Listen to the error event to deal with connection failure or parsing errors, distinguish error types and execute corresponding logic, such as network problems relying on automatic reconnection, server errors manually delay reconnection, and authentication failure refresh token; 3. Actively control the reconnection logic, such as manually closing and rebuilding the connection, setting the maximum number of retry times, combining navigator.onLine to judge network status to optimize the retry strategy. These measures can improve application stability and user experience.

HTML5, CSS and JavaScript should be efficiently combined with semantic tags, reasonable loading order and decoupling design. 1. Use HTML5 semantic tags, such as improving structural clarity and maintainability, which is conducive to SEO and barrier-free access; 2. CSS should be placed in, use external files and split by module to avoid inline styles and delayed loading problems; 3. JavaScript is recommended to be introduced in front, and use defer or async to load asynchronously to avoid blocking rendering; 4. Reduce strong dependence between the three, drive behavior through data-* attributes and class name control status, and improve collaboration efficiency through unified naming specifications. These methods can effectively optimize page performance and collaborate with teams.

Server-SentEvents (SSE) is a lightweight solution provided by HTML5 to push real-time updates to the browser. It realizes one-way communication through long HTTP connections, which is suitable for stock market, notifications and other scenarios. Create EventSource instance and listen for messages when using: consteventSource=newEventSource('/stream'); eventSource.onmessage=function(event){console.log('Received message:',event.data);}; The server needs to set Content-Type to text/event

Doctype is a statement that tells the browser which HTML standard to use to parse the page. Modern web pages only need to be written at the beginning of the HTML file. Its function is to ensure that the browser renders the page in standard mode rather than weird mode, and must be located on the first line, with no spaces or comments in front of it; there is only one correct way to write it, and it is not recommended to use old versions or other variants; other such as charset, viewport, etc. should be placed in part.

Using HTML5 semantic tags and Microdata can improve SEO because it helps search engines better understand page structure and content meaning. 1. Use HTML5 semantic tags such as,,,, and to clarify the function of page blocks, which helps search engines establish a more accurate page model; 2. Add Microdata structured data to mark specific content, such as article author, release date, product price, etc., so that search engines can identify information types and use them for display of rich media summary; 3. Pay attention to the correct use of tags to avoid confusion, avoid duplicate tags, test the effectiveness of structured data, regularly update to adapt to changes in schema.org, and combine with other SEO means to optimize for long-term.

It is a block-level element, suitable for layout; it is an inline element, suitable for wrapping text content. 1. Exclusively occupy a line, width, height and margins can be set, which are often used in structural layout; 2. No line breaks, the size is determined by the content, and is suitable for local text styles or dynamic operations; 3. When choosing, it should be judged based on whether the content needs independent space; 4. It cannot be nested and is not suitable for layout; 5. Priority is given to the use of semantic labels to improve structural clarity and accessibility.

When using HTML5Geolocation API to obtain user location, you must first obtain user authorization, and request and explain the purpose at the right time; the basic method is navigator.geolocation.getCurrentPosition(), which contains successful callbacks, wrong callbacks and configuration parameters; common reasons for failure include permission denied, browser not supported, network problems, etc., alternative solutions and clear prompts should be provided. The specific suggestions are as follows: 1. Request permissions when the user operation is triggered, such as clicking the button; 2. Use enableHighAccuracy, timeout, maximumAge and other parameters to optimize the positioning effect; 3. Error handling should distinguish between different errors

Buttons are key interactive elements in HTML forms. There are two main ways to create: 1. Use tags to create custom buttons that support nested content, and the type attribute can be set to submit, reset or button; 2. Use tags to create basic buttons that only display text, suitable for simple scenarios. It is also recommended to combine CSS to set padding, font-size, border-radius and other styles to improve clickability and appearance, and enhance the interactive experience through:hover and:active status. The submission button can also use the disabled attribute to prevent repeated submissions.
