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實(shí)現(xiàn)隱藏在左側(cè)的彈性彈出菜單效果
這篇文章主要介紹了jquery實(shí)現(xiàn)隱藏在左側(cè)的彈性彈出菜單效果,涉及jQuery動(dòng)態(tài)操作頁(yè)面元素的顯示及隱藏技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
JQuery表單驗(yàn)證插件EasyValidator用法分析
這篇文章主要介紹了JQuery表單驗(yàn)證插件EasyValidator用法,以實(shí)例形式系統(tǒng)分析了EasyValidator的功能特性及具體用法,是非常實(shí)用的jQuery插件,需要的朋友可以參考下2014-11-11
jQuery之$(document).ready()使用介紹
學(xué)習(xí)jQuery的第一件事是:如果你想要一個(gè)事件運(yùn)行在你的頁(yè)面上,你必須在$(document).ready()里調(diào)用這個(gè)事件2012-04-04
jQuery對(duì)JSON數(shù)據(jù)進(jìn)行排序輸出的方法
這篇文章主要介紹了jQuery對(duì)JSON數(shù)據(jù)進(jìn)行排序輸出的方法,涉及jQuery中g(shù)etJSON與sort等方法的使用技巧,需要的朋友可以參考下2015-06-06
jquery插件jquery.beforeafter.js實(shí)現(xiàn)左右拖拽分隔條對(duì)比圖片的方法
這篇文章主要介紹了jquery插件jquery.beforeafter.js實(shí)現(xiàn)左右拖拽分隔條對(duì)比圖片的方法,可實(shí)現(xiàn)圖片拖拽變換的功能,需要的朋友可以參考下2015-08-08
jQuery焦點(diǎn)圖輪播插件KinSlideshow用法分析
這篇文章主要介紹了jQuery焦點(diǎn)圖輪播插件KinSlideshow用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了jQuery焦點(diǎn)圖輪播插件KinSlideshow的參數(shù)含義與使用方法,需要的朋友可以參考下2016-06-06
基于jQuery實(shí)現(xiàn)一個(gè)marquee無(wú)縫滾動(dòng)的插件
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)一個(gè)marquee無(wú)縫滾動(dòng)的插件,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
jQuery插件StickUp實(shí)現(xiàn)網(wǎng)頁(yè)導(dǎo)航置頂
本文給大家介紹的是一款jQuery插件--StickUp,他的主要用途是實(shí)現(xiàn)網(wǎng)頁(yè)元素固定,如導(dǎo)航固定讓其總是保持在視圖中可見(jiàn),效果非常不錯(cuò),這里推薦給小伙伴們。2015-04-04

