jQuery学習ノート(2)
22525 ワード
val()
マウスを置くとテキストが消え、マウスが離すとテキストが復元されます
効果図:
code as below:
View Code
マウスを置くとテキストが消え、マウスが離すとテキストが復元されます
効果図:
code as below:
1 <html xmlns="http://www.w3.org/1999/xhtml">
2 <head runat="server">
3 <script src="Scripts/jquery-1.11.2.js" type="text/javascript"></script>
4 <script type="text/javascript">
5
6 $(function () {
7 //var p_html = $("p").html(); //html() js innerHTML
8 //alert(p_html);
9
10 //var p_text = $("p").text(); //text() js innerText
11 //alert(p_text);
12
13
14 //
15 $("#address").focus(function () { // focus() js onfocus()
16 var txt_value = $(this).val();
17 if (txt_value == " ") {
18 $(this).val("");
19 }
20 });
21
22 $("#address").blur(function () { // blur() js onblur()
23 var txt_value = $(this).val();
24 if (txt_value == "") {
25 $(this).val(" ");
26 }
27 });
28
29 //
30 $("#password").focus(function () {
31 var txt_value = $(this).val();
32 if (txt_value == " ") {
33 $(this).val("");
34 }
35 });
36
37 $("#password").blur(function () {
38 var txt_value = $(this).val();
39 if (txt_value == "") {
40 $(this).val(" ");
41 }
42 });
43
44
45 // defaultValue
46 //
47 $("#address").focus(function () {
48 var txt_value = $(this).val();
49 if (txt_value == this.defaultValue) { //this.defaultValue
50 $(this).val("");
51 }
52
53 });
54 $("#address").blur(function () {
55 var txt_value = $(this).val();
56 if (txt_value == "") { //this.defaultValue
57 $(this).val(this.defaultValue);
58 }
59
60 });
61
62 //
63
64 });
65 </script>
66 <title></title>
67 </head>
68 <body>
69 <form id="form1" runat="server">
70 <div>
71 <p title=" ">
72 <strong> ?</strong></p>
73 <input type="text" id="address" value=" " /><br />
74 <input type="text" id="password" value=" " /><br />
75 <input type="button" value=" "/>
76 </div>
77 </form>
78 </body>
79 </html>
View Code