<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Use of JQ focus selector</title> <link rel="stylesheet" href="base.css" /> <script type="text/javascript" src="jquery-1.10.1.min.js" ></script> </head> <body> <div style="width: 200px; margin: 20px auto 0 auto;"> 姓名:<input name="name" type="text" label="姓名"><br> 年龄:<input name="age" type="text" label="年龄"><br> 手机:<input name="cellphone" type="text" label="手机"> </div> <div id="message" style=" width: 200px; margin: 0 auto;"></div> <script> //: The focus selector is generally used in event processing to determine whether the specified element currently has the focus. //In general, only elements that receive keyboard events or user input can gain focus, such as a form's <input> element. $("input").on("focus blur", function(){ var me = $(this); //When the <input> element gets the focus, the corresponding prompt information such as "Please enter [name]", "Please enter [age]" is displayed in #message. var msg = me.is(":focus") ? ("请输入[" + me.attr("label") + "]") : ""; $("#message").html(msg); }); </script> </body> </html>
Effect picture: