jQuery設定和擷取HTML、文本和值

來源:互聯網
上載者:User

標籤:style   blog   color   java   io   strong   ar   div   cti   

jQuery設定和擷取HTML、文本和值按 Ctrl+C 複製代碼<script type="text/javascript"> //<![CDATA[  $(function(){      //擷取<p>元素的HTML代碼      $("input:eq(0)").click(function(){            alert(  $("p").html() );      });      //擷取<p>元素的文本      $("input:eq(1)").click(function(){            alert(  $("p").text() );      });      //設定<p>元素的HTML代碼      $("input:eq(2)").click(function(){             $("p").html("<strong>你最喜歡的水果是?</strong>");      });           //設定<p>元素的文本      $("input:eq(3)").click(function(){             $("p").text("你最喜歡的水果是?");      });        //設定<p>元素的文本      $("input:eq(4)").click(function(){             $("p").text("<strong>你最喜歡的水果是?</strong>");      });        //擷取按鈕的value值      $("input:eq(5)").click(function(){             alert( $(this).val() );      });         //設定按鈕的value值      $("input:eq(6)").click(function(){            $(this).val("我被點擊了!");      });    });  //]]>  </script>按 Ctrl+C 複製代碼 <script type="text/javascript"> //<![CDATA[  $(function(){      $("#address").focus(function(){         // 地址框獲得滑鼠焦點            var txt_value =  $(this).val();   // 得到當前文字框的值            if(txt_value=="請輸入郵箱地址"){                  $(this).val("");              // 如果符合條件,則清空文字框內容            }       });      $("#address").blur(function(){          // 地址框失去滑鼠焦點              var txt_value =  $(this).val();   // 得到當前文字框的值            if(txt_value==""){                $(this).val("請輸入郵箱地址");// 如果符合條件,則設定內容            }       })      $("#password").focus(function(){            var txt_value =  $(this).val();            if(txt_value=="請輸入郵箱密碼"){                $(this).val("");            }       });      $("#password").blur(function(){              var txt_value =  $(this).val();            if(txt_value==""){                $(this).val("請輸入郵箱密碼");            }       })  });  //]]>  </script> <script type="text/javascript"> //<![CDATA[  $(function(){      $("#address").focus(function(){         // 地址框獲得滑鼠焦點            var txt_value =  $(this).val();   // 得到當前文字框的值            if(txt_value==this.defaultValue){                  $(this).val("");              // 如果符合條件,則清空文字框內容            }       });      $("#address").blur(function(){          // 地址框失去滑鼠焦點              var txt_value =  $(this).val();   // 得到當前文字框的值            if(txt_value==""){                $(this).val(this.defaultValue);// 如果符合條件,則設定內容            }       })      $("#password").focus(function(){            var txt_value =  $(this).val();            if(txt_value==this.defaultValue){                $(this).val("");            }       });      $("#password").blur(function(){              var txt_value =  $(this).val();            if(txt_value==""){                $(this).val(this.defaultValue);            }       })  });  //]]>  </script> <script type="text/javascript"> //<![CDATA[  $(function(){      //設定單選下拉框選中      $("input:eq(0)").click(function(){            $("#single").val("2");      });      //設定多選下拉框選中      $("input:eq(1)").click(function(){            $("#multiple").val(["選擇2號", "選擇3號"]);      });      //設定單選框和多選框選中      $("input:eq(2)").click(function(){             $(":checkbox").val(["check2","check3"]);            $(":radio").val(["radio2"]);      });      });  //]]>  </script> <script type="text/javascript"> //<![CDATA[  $(function(){      //設定單選下拉框選中      $("input:eq(0)").click(function(){            $("#single option").removeAttr("selected");  //移除屬性selected            $("#single option:eq(1)").attr("selected",true); //設定屬性selected      });      //設定多選下拉框選中      $("input:eq(1)").click(function(){            $("#multiple option").removeAttr("selected");  //移除屬性selected            $("#multiple option:eq(2)").attr("selected",true);//設定屬性selected            $("#multiple option:eq(3)").attr("selected",true);//設定屬性selected      });      //設定單選框和多選框選中      $("input:eq(2)").click(function(){            $(":checkbox").removeAttr("checked"); //移除屬性checked            $(":radio").removeAttr("checked"); //移除屬性checked            $(":checkbox[value=check2]").attr("checked",true);//設定屬性checked            $("[value=check3]:checkbox").attr("checked",true);//設定屬性checked            $("[value=radio2]:radio").attr("checked",true);//設定屬性checked      });     });  //]]>  </script> :checkbox 表示屬性為checkbox 

 

jQuery設定和擷取HTML、文本和值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.