欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery設(shè)置和獲取HTML、文本和值示例

 更新時(shí)間: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("我被點(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=="請輸入郵箱地址"){ 
$(this).val(""); // 如果符合條件,則清空文本框內(nèi)容
} 
});
$("#address").blur(function(){ // 地址框失去鼠標(biāo)焦點(diǎn)
var txt_value = $(this).val(); // 得到當(dāng)前文本框的值
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(){ // 地址框獲得鼠標(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號", "選擇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)文章

最新評論