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("我被點(diǎn)擊了!");
});
});
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
$(function(){
$("#address").focus(function(){ // 地址框獲得鼠標(biāo)焦點(diǎn)
var txt_value = $(this).val(); // 得到當(dāng)前文本框的值
if(txt_value=="請(qǐng)輸入郵箱地址"){
$(this).val(""); // 如果符合條件,則清空文本框內(nèi)容
}
});
$("#address").blur(function(){ // 地址框失去鼠標(biāo)焦點(diǎn)
var txt_value = $(this).val(); // 得到當(dāng)前文本框的值
if(txt_value==""){
$(this).val("請(qǐng)輸入郵箱地址");// 如果符合條件,則設(shè)置內(nèi)容
}
})
$("#password").focus(function(){
var txt_value = $(this).val();
if(txt_value=="請(qǐng)輸入郵箱密碼"){
$(this).val("");
}
});
$("#password").blur(function(){
var txt_value = $(this).val();
if(txt_value==""){
$(this).val("請(qǐng)輸入郵箱密碼");
}
})
});
//]]>
</script>
<script type="text/javascript"> //<![CDATA[ $(function(){ $("#address").focus(function(){ // 地址框獲得鼠標(biāo)焦點(diǎn) var txt_value = $(this).val(); // 得到當(dāng)前文本框的值 if(txt_value==this.defaultValue){ $(this).val(""); // 如果符合條件,則清空文本框內(nèi)容 } }); $("#address").blur(function(){ // 地址框失去鼠標(biāo)焦點(diǎn) var txt_value = $(this).val(); // 得到當(dāng)前文本框的值 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號(hào)", "選擇3號(hào)"]);
});
//設(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中innerWidth()方法用法實(shí)例
這篇文章主要介紹了jQuery中innerWidth()方法用法,實(shí)例分析了innerWidth()方法的功能、定義及獲取第一個(gè)匹配元素內(nèi)部區(qū)域?qū)挾鹊氖褂眉记?需要的朋友可以參考下2015-01-01
jQuery的ready方法實(shí)現(xiàn)原理分析
這篇文章主要介紹了jQuery的ready方法實(shí)現(xiàn)原理分析的相關(guān)資料,需要的朋友可以參考下2016-10-10
基于jquery實(shí)現(xiàn)最簡(jiǎn)單的選項(xiàng)卡切換效果
這篇文章主要介紹了基于jquery實(shí)現(xiàn)最簡(jiǎn)單的選項(xiàng)卡切換效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2016-05-05
使用jquery給新生的th綁定hover事件的實(shí)例
下面小編就為大家?guī)?lái)一篇使用jquery給新生的th綁定hover事件的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
jQuery?UI旋轉(zhuǎn)器部件Spinner?Widget
這篇文章介紹了jQuery?UI旋轉(zhuǎn)器部件Spinner?Widget,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
用jQuery.ajaxSetup實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)數(shù)據(jù)的過(guò)濾
本文主要對(duì)用jQuery.ajaxSetup實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)數(shù)據(jù)的過(guò)濾的過(guò)程與方法進(jìn)行詳細(xì)全面的實(shí)例講解。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
jquery實(shí)現(xiàn)百葉窗效果(利用li的定位)
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)百葉窗效果,利用li的定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
利用jQuery實(shí)現(xiàn)可輸入搜索文字的下拉框
jQuery實(shí)現(xiàn)點(diǎn)擊下拉框輸入框時(shí),自動(dòng)顯示下面的下拉列表,而且是根據(jù)輸入框中輸入的搜索添加,自動(dòng)在后臺(tái)查找,然后傳到前臺(tái),展現(xiàn)頁(yè)面2013-10-10
JQuery表格內(nèi)容過(guò)濾的實(shí)現(xiàn)方法
使用JQuery 實(shí)現(xiàn)一個(gè)表格內(nèi)容 過(guò)濾的功能,有需要的朋友可以參考一下,代碼如下:2013-07-07

