jQuery設(shè)置和獲取HTML、文本和值示例
更新時間:2014年07月08日 17:53:09 投稿:whsnow
這篇文章主要介紹了jQuery如何設(shè)置和獲取HTML、文本和值,需要的朋友可以參考下
<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() ); }); //設(shè)置<p>元素的HTML代碼 $("input:eq(2)").click(function(){ $("p").html("<strong>你最喜歡的水果是?</strong>"); }); //設(shè)置<p>元素的文本 $("input:eq(3)").click(function(){ $("p").text("你最喜歡的水果是?"); }); //設(shè)置<p>元素的文本 $("input:eq(4)").click(function(){ $("p").text("<strong>你最喜歡的水果是?</strong>"); }); //獲取按鈕的value值 $("input:eq(5)").click(function(){ alert( $(this).val() ); }); //設(shè)置按鈕的value值 $("input:eq(6)").click(function(){ $(this).val("我被點擊了!"); }); }); //]]> </script>
<script type="text/javascript"> //<![CDATA[ $(function(){ $("#address").focus(function(){ // 地址框獲得鼠標焦點 var txt_value = $(this).val(); // 得到當前文本框的值 if(txt_value=="請輸入郵箱地址"){ $(this).val(""); // 如果符合條件,則清空文本框內(nèi)容 } }); $("#address").blur(function(){ // 地址框失去鼠標焦點 var txt_value = $(this).val(); // 得到當前文本框的值 if(txt_value==""){ $(this).val("請輸入郵箱地址");// 如果符合條件,則設(shè)置內(nèi)容 } }) $("#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(""); // 如果符合條件,則清空文本框內(nèi)容 } }); $("#address").blur(function(){ // 地址框失去鼠標焦點 var txt_value = $(this).val(); // 得到當前文本框的值 if(txt_value==""){ $(this).val(this.defaultValue);// 如果符合條件,則設(shè)置內(nèi)容 } }) $("#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(){ //設(shè)置單選下拉框選中 $("input:eq(0)").click(function(){ $("#single").val("2"); }); //設(shè)置多選下拉框選中 $("input:eq(1)").click(function(){ $("#multiple").val(["選擇2號", "選擇3號"]); }); //設(shè)置單選框和多選框選中 $("input:eq(2)").click(function(){ $(":checkbox").val(["check2","check3"]); $(":radio").val(["radio2"]); }); }); //]]> </script>
<script type="text/javascript"> //<![CDATA[ $(function(){ //設(shè)置單選下拉框選中 $("input:eq(0)").click(function(){ $("#single option").removeAttr("selected"); //移除屬性selected $("#single option:eq(1)").attr("selected",true); //設(shè)置屬性selected }); //設(shè)置多選下拉框選中 $("input:eq(1)").click(function(){ $("#multiple option").removeAttr("selected"); //移除屬性selected $("#multiple option:eq(2)").attr("selected",true);//設(shè)置屬性selected $("#multiple option:eq(3)").attr("selected",true);//設(shè)置屬性selected }); //設(shè)置單選框和多選框選中 $("input:eq(2)").click(function(){ $(":checkbox").removeAttr("checked"); //移除屬性checked $(":radio").removeAttr("checked"); //移除屬性checked $(":checkbox[value=check2]").attr("checked",true);//設(shè)置屬性checked $("[value=check3]:checkbox").attr("checked",true);//設(shè)置屬性checked $("[value=radio2]:radio").attr("checked",true);//設(shè)置屬性checked }); }); //]]> </script>
:checkbox 表示屬性為checkbox
相關(guān)文章
jQuery?UI旋轉(zhuǎn)器部件Spinner?Widget
這篇文章介紹了jQuery?UI旋轉(zhuǎn)器部件Spinner?Widget,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06用jQuery.ajaxSetup實現(xiàn)對請求和響應(yīng)數(shù)據(jù)的過濾
本文主要對用jQuery.ajaxSetup實現(xiàn)對請求和響應(yīng)數(shù)據(jù)的過濾的過程與方法進行詳細全面的實例講解。具有很好的參考價值,需要的朋友一起來看下吧2016-12-12